如何在多处理程序中退出整个应用程序

时间:2012-06-16 14:59:06

标签: python multiprocessing exit

以下是示例代码,就像所描述的代码一样,我想在一个正在运行的处理中退出整个应用程序。但是当我调用 exit(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()

2 个答案:

答案 0 :(得分:0)

您可以在流程列表中的所有内容上调用.terminate()。这通常是一个坏主意,因为您的流程的子流程将变为孤立。

来自multiprocessing

  

terminate()终止进程。在Unix上,这是使用   SIGTERM信号;在Windows上使用TerminateProcess()。请注意退出   处理程序和最后的条款等将不会被执行。

     

请注意,该进程的后代进程不会被终止 -   他们只会成为孤儿。

在你的情况下,它看起来像:

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()

答案 1 :(得分: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!

这里process-1设置mp.Event:

[INFO/Process-1] SETTING sig

这里的过程认识到已经设置了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