2012-06-16 64 views
1

以下是示例代碼,就像所描述的代碼一樣,我希望在一個正在運行的處理中退出整個應用程序。但是當我調用退出(0)函數時,其他一些處理仍在運行。那麼如何同時殺死所有正在運行的進程呢?任何幫助感激!如何在多處理程序中退出整個應用程序

from multiprocessing import Process 
def submit_process(): 
    sig = False 

    #Here is some codes trying to change the value of the variable sig 

    print "submit_process" 

    if(sig == True): 
     #Here i want to exit the entire program instead of the single thread only. 
     #exit(0) 
     pass 

process_list = [] 
print "OK" 
for i in range(10): 
    process = Process(target = submit_process) 
    process.start() 
    process_list.append(process) 

for i in range(len(process_list)): 
    process_list[i].join() 

回答

0

您可以在進程列表中的所有內容上調用.terminate()。這通常是一個糟糕的主意,儘管你的進程的子進程將會變成孤兒。

multiprocessing

終止()終止進程。在Unix上,這是通過使用 SIGTERM信號完成的;在Windows上使用TerminateProcess()。請注意,退出 處理程序和finally子句等將不會執行。

請注意,該進程的後代進程將不會終止 - 他們將簡單地成爲孤兒。

在你的情況下,它看起來是這樣的:

from multiprocessing import Process 
def submit_process(): 
    sig = False 

    #Here is some codes trying to change the value of the variable sig 

    print "submit_process" 

    if(sig == True): 
     # Terminate all spawned processes 
     for process in process_list: 
      process.terminate() 
     exit(0) 

process_list = [] 
print "OK" 
for i in range(10): 
    process = Process(target = submit_process) 
    process.start() 
    process_list.append(process) 

for i in range(len(process_list)): 
    process_list[i].join() 
0

如果處理功能(如submit_process)有一個循環,你可以測試一個條件,你可以使用一個mp.Event此:

import multiprocessing as mp 
import time 
import logging 

logger = mp.log_to_stderr(logging.DEBUG) 
logger.setLevel(logging.INFO) 

def submit_process(i, sig): 
    logger.info("submit_process") 
    while True: 
     #Here is some codes trying to change the value of the variable sig 
     if i == 0: 
      time.sleep(2) 
      sig.set() 
      logger.info('SETTING sig') 
     time.sleep(1) 
     if sig.is_set(): 
      #Here i want to exit the entire program instead of the single thread only. 
      #exit(0) 
      logger.info('sig is set!') 
      break 
     else: 
      logger.info('sig is NOT set!')    

process_list = [] 
logger.info("OK") 
sig = mp.Event() 
for i in range(3): 
    process = mp.Process(target = submit_process, args = (i, sig,)) 
    process.start() 
    process_list.append(process) 

for i in range(len(process_list)): 
    process_list[i].join() 

產生

[INFO/MainProcess] OK 
[INFO/Process-2] child process calling self.run() 
[INFO/Process-2] submit_process 
[INFO/Process-3] child process calling self.run() 
[INFO/Process-1] child process calling self.run() 
[INFO/Process-3] submit_process 
[INFO/Process-1] submit_process 
[INFO/Process-2] sig is NOT set! 
[INFO/Process-3] sig is NOT set! 
[INFO/Process-2] sig is NOT set! 

這裏過程-1設置mp.Event:

[INFO/Process-1] SETTING sig 

這裏的處理識別出的簽名已被設置並跳出while循環的:

[INFO/Process-3] sig is set! 
[INFO/Process-3] process shutting down 
[INFO/Process-3] process exiting with exitcode 0 
[INFO/Process-2] sig is set! 
[INFO/Process-2] process shutting down 
[INFO/Process-2] process exiting with exitcode 0 
[INFO/Process-1] sig is set! 
[INFO/Process-1] process shutting down 
[INFO/Process-1] process exiting with exitcode 0 
[INFO/MainProcess] process shutting down