[英]Cleanup child processes in multiprocessing
Consider simple setup of a child process.考虑一个子进程的简单设置。 Basically, it is a producer(parent)-consumer(child) scenario.
基本上,这是一个生产者(父母)-消费者(孩子)的场景。
class Job:
def start_process(self):
self.queue = multiprocessing.Queue(3)
self.process = multiprocessing.Process(target=run,
args=(self.queue))
def run(queue):
while True:
item = queue.get()
....
If I do kill -9
on the parent process the child will hang forever.如果我在父进程上执行
kill -9
,则子进程将永远挂起。 I was sure that it will receive SIGHUP
like with subprocess.Popen
- when the python process quits the popen
ed will quit as well.我确信它会像
subprocess.Popen
一样接收SIGHUP
- 当 python 进程退出时, popen
ed也会退出。 Any idea how to fix child cleanup?知道如何解决儿童清理问题吗?
If the daemon
param doesn't work for you, you can catch a SIGINT signal and have it set a boolean value to exit the while loop in your children.如果
daemon
参数对您不起作用,您可以捕获一个 SIGINT 信号并让它设置一个 boolean 值以退出您的孩子的 while 循环。 ie.. IE..
import signal
g_run_loops = True
def signal_handler(signum, frame):
global g_run_loops
g_run_loops = False
signal.signal(signal.SIGINT, signal_handler)
def run(queue):
global g_run_loops
while g_run_loops:
item = queue.get()
....
Note that this won't work for SIGKILL (kill -9) but should work for SIGINT (kill -2).请注意,这不适用于 SIGKILL (kill -9),但应该适用于 SIGINT (kill -2)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.