<legend id='94yBz'><style id='94yBz'><dir id='94yBz'><q id='94yBz'></q></dir></style></legend>

    <small id='94yBz'></small><noframes id='94yBz'>

    <tfoot id='94yBz'></tfoot>
  • <i id='94yBz'><tr id='94yBz'><dt id='94yBz'><q id='94yBz'><span id='94yBz'><b id='94yBz'><form id='94yBz'><ins id='94yBz'></ins><ul id='94yBz'></ul><sub id='94yBz'></sub></form><legend id='94yBz'></legend><bdo id='94yBz'><pre id='94yBz'><center id='94yBz'></center></pre></bdo></b><th id='94yBz'></th></span></q></dt></tr></i><div id='94yBz'><tfoot id='94yBz'></tfoot><dl id='94yBz'><fieldset id='94yBz'></fieldset></dl></div>

      <bdo id='94yBz'></bdo><ul id='94yBz'></ul>
      1. 如何终止多处理池进程?

        时间:2023-05-26
          <tbody id='h1OyY'></tbody>

            <small id='h1OyY'></small><noframes id='h1OyY'>

            <i id='h1OyY'><tr id='h1OyY'><dt id='h1OyY'><q id='h1OyY'><span id='h1OyY'><b id='h1OyY'><form id='h1OyY'><ins id='h1OyY'></ins><ul id='h1OyY'></ul><sub id='h1OyY'></sub></form><legend id='h1OyY'></legend><bdo id='h1OyY'><pre id='h1OyY'><center id='h1OyY'></center></pre></bdo></b><th id='h1OyY'></th></span></q></dt></tr></i><div id='h1OyY'><tfoot id='h1OyY'></tfoot><dl id='h1OyY'><fieldset id='h1OyY'></fieldset></dl></div>
            • <tfoot id='h1OyY'></tfoot>

              <legend id='h1OyY'><style id='h1OyY'><dir id='h1OyY'><q id='h1OyY'></q></dir></style></legend>
                <bdo id='h1OyY'></bdo><ul id='h1OyY'></ul>

                  本文介绍了如何终止多处理池进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在开发渲染农场,我需要我的客户能够启动渲染器的多个实例,而不会阻塞,以便客户端可以接收新命令.我的工作正常,但是在终止创建的进程时遇到问题.

                  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)
                  

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

                  上一篇:使用多处理时出现 PicklingError 下一篇:使用 multiprocessing.pool.map 传递 kwargs

                  相关文章

                  最新文章

                      <bdo id='fkvNz'></bdo><ul id='fkvNz'></ul>

                    <small id='fkvNz'></small><noframes id='fkvNz'>

                      <i id='fkvNz'><tr id='fkvNz'><dt id='fkvNz'><q id='fkvNz'><span id='fkvNz'><b id='fkvNz'><form id='fkvNz'><ins id='fkvNz'></ins><ul id='fkvNz'></ul><sub id='fkvNz'></sub></form><legend id='fkvNz'></legend><bdo id='fkvNz'><pre id='fkvNz'><center id='fkvNz'></center></pre></bdo></b><th id='fkvNz'></th></span></q></dt></tr></i><div id='fkvNz'><tfoot id='fkvNz'></tfoot><dl id='fkvNz'><fieldset id='fkvNz'></fieldset></dl></div>
                      <tfoot id='fkvNz'></tfoot>
                    1. <legend id='fkvNz'><style id='fkvNz'><dir id='fkvNz'><q id='fkvNz'></q></dir></style></legend>