multithreading - Running program/function in background in Python -
i'm trying run program/function (whichever possible), python program, start , close. example. python script "parent.py" called input, input data passed function/program "child", take around 30 minutes complete. parent.py should closed after calling "child".
is there way that?
right i'm able start child, parent closed after completion of child. which, want avoid.
as understand it, goal start background process in subprocess not have main program wait finish. here example program that:
$ cat script.py import subprocess subprocess.popen("sleep 3; echo 'done!';", shell=true)
here example of program in operation:
$ python script.py $ $ $ done!
as can see, shell script continued run after python exited.
subprocess
has many options , want customize subprocess call needs.
in cases, child process lives after parent exits may leave zombie process. instructions on how avoid that, see here.
the alternative: making subprocess wait
if want opposite happen python waiting subprocess complete, @ program:
$ cat script.py import subprocess p = subprocess.popen("sleep 3; echo 'done!';", shell=true) p.wait()
here example output:
$ python script.py done! $ $
as can see, because called wait
, python waited until subprocess completed before exiting.
Comments
Post a Comment