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?

Joey B
  • 125
  • 3
  • 14
  • As a note, I am working on a PC right now and will switch to my mac as soon as I get back home. – Joey B Jun 27 '17 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 :) – EvgenyKolyakov Jun 27 '17 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? – Joey B Jun 27 '17 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. – Jean-François Fabre Jun 27 '17 at 18:45
  • maybe you'd be better off with 1 script and threads. – Jean-François Fabre Jun 27 '17 at 18:45
  • Okay, so script1 is collecting data using an API. Script2 then plots the data collected and updates whenever script1 gets new data. As I run it now, I can watch the data collection (as it has print statements) but the chart from script2 will not open until script1 terminates. I am unfamiliar with threads, would that cause a headache to recode? – Joey B Jun 27 '17 at 18:47
  • @JoeyB, Try to wrap each of the two lines in place of XXXX: `bash -c "XXXXX"` – Ayman Nedjmeddine Jun 27 '17 at 18:55
  • Not sure I did it right but doing that makes nothing run -- bash -c "python Charts.py &" \n bash -c "python API.py &" -- Is that what its supposed to look like? – Joey B Jun 27 '17 at 18:58

3 Answers3

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()
user590028
  • 11,364
  • 3
  • 40
  • 57
  • I am on windows 7 and unfortunately, this solution does not work for me. It is still only running one script at a time. – Joey B Jun 27 '17 at 19:14
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?

Joey B
  • 125
  • 3
  • 14
  • Joey -- take a look at the python solution below. This WILL work on all python platforms, including Windows & OSX. – user590028 Jun 29 '17 at 17:44
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)

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134