我见过的几个问题,如这一个,而是试图检查的各种变种如果孩子还活着,并退出子进程后,我简化了这个问题,它仍然是行不通的。Python的派生进程不会死
我在用sys.exit(0)退出分支进程时出错了吗? 是否有另一种杀死它的方法。事情是,我不能让父母杀死进程,因为它不知道他们什么时候完成工作。
起初我以为这是因为在退出之前执行了一个系统命令(Python run system command and then exit... won't exit),但我甚至在简化版本中删除了它,因为给定的解决方案也不起作用。
下面是一个例子:
import sys
import os
import time
children = []
for i in range(0,3):
pid = os.fork()
if pid == -1:
continue
elif pid == 0:
# Do work...
print 'Child %d spawned' % os.getpid()
sys.exit(0)
else:
children.append(pid)
time.sleep(5)
for pid in children:
proc_path = '/proc/%d' % pid
if not os.path.exists(proc_path):
print 'Child %d is dead' % pid
else:
print 'Child %d is alive' % pid
此打印:
Child 27636 spawned
Child 27637 spawned
Child 27638 spawned
Child 27636 is alive
Child 27637 is alive
Child 27638 is alive
但子进程应该是死了。
在这种情况下是什么导致这些进程成为僵尸?
谢谢。这工作,我将不得不添加更多的逻辑,但没关系。 – JayLev 2012-04-13 11:28:03