如何终止多处理池进程?

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)

这篇关于如何终止多处理池进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

build conda package from local python package(从本地 python 包构建 conda 包)
How can I see all packages that depend on a certain package with PIP?(如何使用 PIP 查看依赖于某个包的所有包?)
How to organize multiple python files into a single module without it behaving like a package?(如何将多个 python 文件组织到一个模块中而不像一个包一样?)
Check if requirements are up to date(检查要求是否是最新的)
How to upload new versions of project to PyPI with twine?(如何使用 twine 将新版本的项目上传到 PyPI?)
Why #egg=foo when pip-installing from git repo(为什么从 git repo 进行 pip 安装时 #egg=foo)