我見過的幾個問題,如這一個,而是試圖檢查的各種變種如果孩子還活着,並退出子進程後,我簡化了這個問題,它仍然是行不通的。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