From the python terminal, I run some command like the following, to spawn a long-running child process:
from multiprocessing.process import Process
Process(target=LONG_RUNNING_FUNCTION).start()
This command returns, and I can do other things in the python terminal, but anything printed by the child is still printed to my python terminal session.
When I exit the terminal (either with exit
or CTRL+D), the exit command it hangs. If I hit CTRL+C during this hang, the child process is terminated.
If I kill the python terminal process manually (via the posix kill
command), the child process is instead orphaned, and continues running with its output presumably discarded.
If I run this code with python -c
, it waits for the child to terminate, and CTRL+C kills both parent and child.
Which run configurations of python kill children when the parents are terminated? In particular, if a python-mod_wsgi-apache webserver spawns child processes and then is restarted, are the children killed?
[ As an aside, what is the proper way of detaching child processes spawned from the terminal? Is there a way more elegant than the following: Deliberately make an orphan process in python ]
Update: python subprocesses spawned with multiprocessing.Process
by a web server running under apache are not killed when apache is restarted.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…