As soon as it subprocess.callreturns, the subprocess is executed - and the callreturn value is a subprocess returncode. Thus, accumulating these return codes in the list pids(which is not synchronized between multiprocess addition and the main process) and sending them 9signals “as if”, they were process identifiers instead of returning the codes, of course, are erroneous.
Another thing with a question that is definitely incorrect is the specification:
kill -9 parent_process_pid".
-9 , ( -9) - , -9 .
threading multiprocessing ( "babysitter", , , , ?); suprocess.Process ( .pid ) babysitter, ( ). , , , , , ( ), , .
, ( , ;-) s/thing, :
import subprocess, threading, signal
import sys, time
pobs = set()
pobslock = threading.Lock()
def numpobs():
with pobslock:
return len(pobs)
def sigterm_handler(signal, frame):
print 'You killed me!'
with pobslock:
for p in pobs: p.kill()
sys.exit(0)
def sigint_handler(signal, frame):
print 'You pressed Ctrl+C!'
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigterm_handler)
def f_wrapper(d, p):
print d, 'start', p.pid
rc = p.wait()
with pobslock:
pobs.remove(p)
print d, 'done, rc =', rc
print "Starting to run things."
for i in range(5):
p = subprocess.Popen(['sleep', '100'])
with pobslock:
pobs.add(p)
t = threading.Thread(target=f_wrapper, args=(i, p))
t.daemon=True
t.start()
print "Got things running ..."
while numpobs():
print "Still working ..."
time.sleep(1)