0

I have 2 scripts that I need to run at the same time. One script collects data and the other plots the data live.

On PC, I can simply open 2 IDLE shells and they will run concurrently but on Mac, that isn't possible.

I wrote the following bash file as suggested in this post (Run multiple python scripts concurrently):

python script1.py &
python script2.py &

But this only runs my scripts one at a time. Is there anyway on a mac that I can get both scripts running at the same time?

8
  • As a note, I am working on a PC right now and will switch to my mac as soon as I get back home. Commented Jun 27, 2017 at 18:38
  • you can write it in one line (with spaces around &) and you can run like this as many processes as your line can take :) Commented Jun 27, 2017 at 18:38
  • I am still seeing the same problem, where it will only run a single script at a time. It will run script1.py and once I kill that script, it will move onto script2.py. I need them running both simultaneously because one relies on the other. Does that make sense? Commented Jun 27, 2017 at 18:45
  • you're starting 2 processes in background, so they are actually running at the same time. Convince yourself by adding some prints to your scripts. Commented Jun 27, 2017 at 18:45
  • maybe you'd be better off with 1 script and threads. Commented Jun 27, 2017 at 18:45

3 Answers 3

1

You can do it all from within python by using subprocess.Popen()

import subprocess
import sys

s1 = subprocess.Popen([sys.executable, 'script1.py'])
s2 = subprocess.Popen([sys.executable, 'script2.py'])
s1.wait()
s2.wait()
Sign up to request clarification or add additional context in comments.

1 Comment

I am on windows 7 and unfortunately, this solution does not work for me. It is still only running one script at a time.
1

For my purposes, I was able to find a workaround that's slightly more tedious. I have 2 separate bash scripts now, each containing one of the lines from the above script I initially posted. Running both the bash scripts will run both my scripts simultaneously in different shells.

As a side note, does anybody know how I can do a similar thing, where I use a single bash script to call both of the new bash scripts?

1 Comment

Joey -- take a look at the python solution below. This WILL work on all python platforms, including Windows & OSX.
0

That's not true, on OS X (Mac) works as expected.

script1.py

#! /usr/bin/env python
import time
time.sleep(1)
print "script1.py"

script2.py

#! /usr/bin/env python
print "script2.py"

run

set executable permission and run in shell

./script1.py &
./script2.py &

and the output will be

script2.py
script1.py

proving that both were run concurrently (as output from second script is displayed first)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.