Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python in a separate process

Tags:

python

process

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.

like image 921
Bialecki Avatar asked Jun 02 '10 01:06

Bialecki


People also ask

How do you start a separate process in python?

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.

How many process can I run in Python?

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.


2 Answers

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()
like image 98
Shane C. Mason Avatar answered Sep 21 '22 08:09

Shane C. Mason


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 &
like image 37
Michael Aaron Safyan Avatar answered Sep 20 '22 08:09

Michael Aaron Safyan