Running python in a separate process

I am looking for a quick bash script or program that will allow me to run a python script in a separate process. What is the best way to do this? I know this is incredibly simple, just curious if there is a preferred way to do this.

+4
source share
4 answers

Just use ampersand (&) to start the Python process in the background. Python is already running in a separate process from the BASH script, so saying that to run it "in a separate thread" does not make much sense - I assume that you just want it to run in the background:

#! /bin/bash python path/to/python/program.py & 

Please note that the above may cause the text to be printed on the console. You can work around this using redirection to redirect both stdout and stderr to a file. For instance:

 #! /bin/bash python path/to/python/program.py > results.txt 2> errors.log & 
+13
source

The best way to do this is to do it in python! Take a look at multiprocessor 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() 
+4
source

Your jargon is confused. But in bash, you can run the process in the background by adding & :

 print foo.py & 
+2
source

bash doesn't really work with threads - it really makes processes just fine:

 python whatever.py & 

& in the end simply means β€œdon't wait for the subprocess to complete” - bash will execute this command in a separate process anyway, it's just that it usually waits for this separate terminate process (all Unix shells have been working this way from time immemorial).

+2
source

Source: https://habr.com/ru/post/1311576/


All Articles