How to terminate multiprocessing Pool processes?(如何终止多处理池进程?)
问题描述
我正在开发渲染农场,我需要我的客户能够启动渲染器的多个实例,而不会阻塞,以便客户端可以接收新命令.我的工作正常,但是在终止创建的进程时遇到问题.
I'm working on a renderfarm, and I need my clients to be able to launch multiple instances of a renderer, without blocking so the client can receive new commands. I've got that working correctly, however I'm having trouble terminating the created processes.
在全局级别,我定义了我的池(以便我可以从任何函数访问它):
At the global level, I define my pool (so that I can access it from any function):
p = Pool(2)
然后我用 apply_async 调用我的渲染器:
I then call my renderer with apply_async:
for i in range(totalInstances):
p.apply_async(render, (allRenderArgs[i],args[2]), callback=renderFinished)
p.close()
该函数完成,在后台启动进程,并等待新命令.我做了一个简单的命令,它将杀死客户端并停止渲染:
That function finishes, launches the processes in the background, and waits for new commands. I've made a simple command that will kill the client and stop the renders:
def close():
'''
close this client instance
'''
tn.write ("say "+USER+" is leaving the farm
")
try:
p.terminate()
except Exception,e:
print str(e)
sys.exit()
它似乎没有给出错误(它会打印错误),python 终止但后台进程仍在运行.谁能推荐一种更好的方法来控制这些已启动的程序?
It doesn't seem to give an error (it would print the error), the python terminates but the background processes are still running. Can anyone recommend a better way of controlling these launched programs?
推荐答案
找到了我自己问题的答案.主要问题是我调用的是第三方应用程序而不是函数.当我调用子进程[使用 call() 或 Popen()] 时,它会创建一个新的 python 实例,其唯一目的是调用新的应用程序.但是当 python 退出时,它会杀死这个新的 python 实例并让应用程序继续运行.
Found the answer to my own question. The primary problem was that I was calling a third-party application rather than a function. When I call the subprocess [either using call() or Popen()] it creates a new instance of python whose only purpose is to call the new application. However when python exits, it will kill this new instance of python and leave the application running.
解决方案是通过找到所创建的 python 进程的 pid,获取该 pid 的子进程并杀死它们来执行此操作.此代码特定于 osx;有更简单的代码(不依赖于 grep)可用于 linux.
The solution is to do it the hard way, by finding the pid of the python process that is created, getting the children of that pid, and killing them. This code is specific for osx; there is simpler code (that doesn't rely on grep) available for linux.
for process in pool:
processId = process.pid
print "attempting to terminate "+str(processId)
command = " ps -o pid,ppid -ax | grep "+str(processId)+" | cut -f 1 -d " " | tail -1"
ps_command = Popen(command, shell=True, stdout=PIPE)
ps_output = ps_command.stdout.read()
retcode = ps_command.wait()
assert retcode == 0, "ps command returned %d" % retcode
print "child process pid: "+ str(ps_output)
os.kill(int(ps_output), signal.SIGTERM)
os.kill(int(processId), signal.SIGTERM)
这篇关于如何终止多处理池进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何终止多处理池进程?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01