Running python scrips at the same time with bash - python

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?

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()

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?

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)

Related

How to run two python programs on MacBook simultaneously?

I was wondering if I need to run two python programs simultaneously, can I open two terminal windows and run each of them at the same time? Will this slow down the computing? If so, is there any way to quickly run several programs at the same time? Like parallel computing. Cheers.
Can I open two terminal windows and run each of them at the same time?
You can do that but they won't run parallelly.
Will this slow down the computing?
If you can run them in a truly parallel way, individual programs should not feel any slower than if they were run sequentially.
However, if you have multiple python scripts that you wish to run simultaneously in the same terminal, you can type the following lines on your bash terminal:
python3 script1.py &
python3 script2.py &
Or if you want to run them from another python script, you can do this:
# script3.py
import subprocess
subprocess.run("python3 script1.py & python3 script2.py", shell=True)
Or if you want to run multiple processes parallelly, you can take a look at python's built in multiprocessing module.
Option1: Check using GNU Screen with vertical split
Option2: Download iTerm2 for macOSX from this link.

Can I run a shell command from python script in the current shell

I have a python script which invokes two different shell scripts. The first script sets some environment variables which are required by the second script. The python code has the following structure :
import subprocess
subprocess.call(["bash", "a.sh"]) #a.sh sets env_var1
subprocess.call(["bash", "b.sh"]) #b.sh reads env_var1
Because the scripts a.sh and b.sh run in different shells, the above code does not do the needful.
I want to know that can we execute these shell scripts from python in the current shell itself
You can use this line to run commands to your shell in python os.system('command to run here')
In your case it would be something like:
import os
os.system('./a.sh')
os.system('./b.sh')

How to run multiple Python scripts from command line?

I have ten python scripts in the same directory. How to run all of these from command line, that it will work in background?
I use SSH terminal to connect to server CentOS and run Python script as:
python index.py
But when I close client terminal SSH, proccess is died
You can use the & command to make things run in the background, and nohup so it continues on logout, such as
nohup python index.py &
If you want to run multiple things this way, it's probably easiest to just make a script to start them all (with a shell of your choice):
#!/bin/bash
nohup python index1.py &
nohup python index2.py &
...
As long as you don't need to interact with the scripts once they are started (and don't need any stdout printing) this could be pretty easily automated with another python script using the subprocess module:
for script in listofscripts:
#use subprocess.run() for python 3.x (this blocks until each script terminates)
subprocess.call(["python", script], *args) #use popen if you want non - blocking
*args is a link (it's coloring got overwritten by code highliting
also of note: stdout / stderr printing is possible, just more work..

Running several programs from one program

I have 12 programs which i intend to run simultaneously. Is there any way i run all of them via one single program which when built runs the 12 programs?
I am using sublime and the programs are python.
If you just want to execute the programs one by one in a batch, you can do it in a bash script. Assuming they are executables in the same folder, you can have an .sh file with the following contents:
#!/bin/bash
python ./my_app1.py &
python ./my_app2.py &
python ./my_app3.py
If the scripts themselves have the #!/usr/bin/env python at the top to identify the interpreter, you can do chmod +x on them, and turn your runner.sh file into:
#!/bin/bash
./my_app1.py &
./my_app2.py &
./my_app3.py
On the other hand, if you want to do this from a python script, you can use:
import subprocess
import os
scripts_to_run = ['my_app1.py', 'my_app2.py', 'my_app3.py']
for s in scripts_to_run:
subprocess.Popen([os.path.join(os.getcwd(), s)])
Note 1: don't forget to include the #!/usr/bin/env python in every script on the first line.
Note 2: it is important to use subprocess.Popen() instead subprocess.call() because the latter is a blocking function that will wait for the application to finish before proceeding. With subproces.Popen() you get the concurrent execution.

Shell script change shell in between

I've a shell script with two shebangs, the first one tells #!/bin/sh and after a few lines the other one is #!/usr/bin/env python.
When this script is given executable permission and ran as ./script.sh, the script works fine, uses /bin/sh in first part and uses python interpreter in latter part.
But when the script is run as sh script.sh, the second shebang is not recognized and the script fails. Is there anyway I can force to change the interpreter if the script is run explicitly as sh script.sh.
The reason I need this is because I need to run the scripts through a tool which runs as sh script.sh
As far as I know you cannot have two shebang lines in one script. The shebang works only when -
it is on the first line
it starts in column one
If you need to run python code then have it in another script and then call the script by doing
python path/to/the/script.py
A better way to do this would be to use a shell here document. Something like this:
#!/bin/sh
curdir=`pwd`
/usr/bin/env python <<EOF
import os
print os.listdir("$curdir")
EOF
This way, you won't need to distribute the code on two separate files.
As you see, you can even access shell variables from the python code.
have your script.sh script invoke the python interpreter directly:
#!/bin/sh
# include full path if needed
python (your python interpreter arguments)

Categories

Resources