I'm looking for a quick bash script or program that will allow me to kick off a python script in a separate process. What's the best way to do this? I know this is incredibly simple, just curious if there's a preferred way to do it.
How to Start a Process in Python? To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.
The maximum number of worker processes may be limited by your operating system. For example, on windows, you will not be able to create more than 61 child processes in your Python program.
The best way to do this is to do it in python! Have a look at the multiprocess libraries.
Here is a simple example from the links above:
from multiprocessing import Process
def f(name):
print 'hello', name
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
Just use the ampersand (&) in order to launch the Python process in the background. Python already is executed in a separate process from the BASH script, so saying to run it "in a separate thread" doesn't make much sense -- I'm assuming you simply want it to run in the background:
#! /bin/bash
python path/to/python/program.py &
Note that the above may result in text being printed to the console. You can get around this by using redirection to redirect both stdout and stderr to a file. For example:
#! /bin/bash
python path/to/python/program.py > results.txt 2> errors.log &
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With