Python多处理:退出时杀死工作程序 [英] Python multiprocessing: Kill worker on exit

查看:76
本文介绍了Python多处理:退出时杀死工作程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的函数来运行具有依赖项跟踪的作业.确切的代码并不重要,但是我的方法是创建一个用multiprocessing.Process进行分叉的作业监视功能,然后使用两个多处理Queue对象向其发送作业和从中获取作业.

I have written a simple function to run jobs with dependency tracking. The exact code isn't important, but the way I did it is to create a job monitoring function that I fork with multiprocessing.Process, I send and get jobs to/from it with two multiprocessing Queue objects.

它很好用,但是因为父进程在退出时挂起,所以我使用了无限循环,因为python仍在等待子进程.有没有一种好的方法可以在退出时立即终止子进程?也许是捕捉到信号了?

It works great, but because I use an infinite loop, when the parent process hangs on exit, because python is still waiting for the child. Is there a good way to kill a child process immediately on exit? Maybe by catching a signal?

我的实际代码在这里: https://gist.github.com/MikeDacre/e672969aff980ee950b9dfa8b2552d40 一个更完整的示例在这里: http://nbviewer.jupyter.org/github /MikeDacre/python-cluster/blob/cluster/tests/local_queue.ipynb

My actual code is here: https://gist.github.com/MikeDacre/e672969aff980ee950b9dfa8b2552d40 A more complete example is here: http://nbviewer.jupyter.org/github/MikeDacre/python-cluster/blob/cluster/tests/local_queue.ipynb

不过,一个玩具示例可能会更好:

A toy example is probably better though:

import multiprocessing as mp
from time import sleep
def runner():
    while True:
        sleep(2)

runner = mp.Process(target=runner)
runner.start()
exit()

在输入Ctrl-C之前,它会愉快地挂起.

That will happily hang until Ctrl-C is entered.

我认为信号捕获不会起作用,因为在正常出口处没有信号发送.有什么办法可以捕捉exit()吗?如果没有,那么有什么方法可以自然终止的方式来创建流程?

I don't think signal catching will work, as there are no signals sent on normal exit. Is there any way to catch exit()? If not is there any way to create a Process in a way that will terminate naturally?

推荐答案

谢谢大家,在写了这个问题后不久,我就找到了使用python的atexit模块的解决方案:

Thanks everyone, shortly after writing this question, I figured out the solution using python's atexit module:

import atexit
import multiprocessing as mp
from time import sleep
def runner():
    while True:
        sleep(2)

def kill_runner(runner):
    runner.terminate()

runner = mp.Process(target=runner)
runner.start()
atexit.register(kill_runner, runner)
exit()

那按预期工作.

这篇关于Python多处理:退出时杀死工作程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆