2012-08-09 57 views
4

我有一個python腳本......基本上調用另一個python腳本。在另一個python腳本里面它產生了一些線程......我如何讓調用腳本等待被調用的腳本完全運行?等到其他python腳本結束

這是我的代碼:

while(len(mProfiles) < num): 
     print distro + " " + str(len(mProfiles)) 
     mod_scanProfiles.main(distro) 
     time.sleep(180) 
     mProfiles = readProfiles(mFile,num,distro) 
     print "yoyo" 

我怎麼做這樣的事情,,,等到mod_scanProfiles.main()和所有線程都徹底完蛋了? (我現在用time.sleep(180),但編程不好)

+0

它看起來不像你使用'subprocess'模塊。你能顯示'mod_scanProfiles.main(distro)'的代碼嗎? – stderr 2012-08-09 13:00:52

+0

我的確沒有使用子流程模塊......我猜測我可能需要使用它才能使用它,所以我也給它加上了標籤 – 2012-08-09 13:02:37

+2

兩種方法:1)改用子流程; 2)改變'mod_scanProfiles.main()'在返回之前等待所有線程完成。無論哪種方式不應該太難。 – 2012-08-09 13:10:51

回答

4

你想修改mod_scanProfiles.main中的代碼,直到它所有的線程都完成爲止。

假設你在函數進行調用subprocess.Popen只是做:

# in mod_scanPfiles.main: 
p = subprocess.Popen(...) 
p.wait() # wait until the process completes. 

如果你目前沒有等待你的線程結束了,你還需要調用Thread.joindocs)等待他們完成。例如:

# assuming you have a list of thread objects somewhere 
threads = [MyThread(), ...] 
for thread in threads: 
    thread.start() 
for thread in threads: 
    thread.join() 
+0

感謝您的示例代碼。我在我的線程列表上使用了連接函數,它現在不用使用子進程而等待:) thnx ...不知道它會那麼簡單... – 2012-08-09 13:30:18