2011-06-23 26 views
1

check.py內容:Python解釋器等待子進程死亡

from multiprocessing import Process 
import time 
import sys 

def slp(): 
time.sleep(30) 
f=open("yeah.txt","w") 
f.close() 

if __name__=="__main__" : 
x=Process(target=slp) 
x.start() 
sys.exit() 

在Windows 7中,從cmd,如果我叫python check.py,它不會立即退出,而是等待30秒。如果我殺了cmd,孩子也死了 - 沒有"yeah.txt"被創建。

我該如何確保孩子繼續跑步,即使父母被殺,父母也不會等待孩子進程結束?

回答

1

你似乎想要的是將腳本作爲後臺進程運行。在How to start a background process in Python?應該這樣做的解決方案,你將不得不指定一些命令行參數,告訴你的腳本進入slp,而不是產生一個新的進程。