1. <tfoot id='pEFQO'></tfoot>

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

        <legend id='pEFQO'><style id='pEFQO'><dir id='pEFQO'><q id='pEFQO'></q></dir></style></legend>
      2. <small id='pEFQO'></small><noframes id='pEFQO'>

        Python 多处理从不加入

        时间:2023-05-25
          • <bdo id='mha4I'></bdo><ul id='mha4I'></ul>
            <tfoot id='mha4I'></tfoot>

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

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

                <tbody id='mha4I'></tbody>

                1. 本文介绍了Python 多处理从不加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 multiprocessing,特别是一个 Pool 来分拆几个线程"来完成我拥有的一堆慢速工作.但是,由于某种原因,我无法让主线程重新加入,即使所有的孩子似乎都已经死了.

                  I'm using multiprocessing, and specifically a Pool to spin off a couple of 'threads' to do a bunch of slow jobs that I have. However, for some reason, I can't get the main thread to rejoin, even though all of the children appear to have died.

                  已解决:看来这个问题的答案是只启动多个 Process 对象,而不是使用 Pool.目前尚不清楚为什么,但我怀疑剩余的进程是池的管理器,并且当进程完成时它并没有死亡.如果其他人有这个问题,这就是答案.

                  Resolved: It appears the answer to this question is to just launch multiple Process objects, rather than using a Pool. It's not abundantly clear why, but I suspect the remaining process is a manager for the pool and it's not dying when the processes finish. If anyone else has this problem, this is the answer.

                  主线程

                  pool = Pool(processes=12,initializer=thread_init)
                  for x in xrange(0,13):
                      pool.apply_async(thread_dowork)
                  pool.close()
                  sys.stderr.write("Waiting for jobs to terminate
                  ")
                  pool.join()
                  

                  xrange(0,13) 比进程数多一,因为我认为我有一个关闭,并且一个进程没有得到工作,所以是'不会死,我想强迫它去工作.我也试过 12 次.

                  The xrange(0,13) is one more than the number of processes because I thought I had an off by one, and one process wasn't getting a job, so wasn't dying and I wanted to force it to take a job. I have tried it with 12 as well.

                  多处理函数

                  def thread_init():
                      global log_out
                      log_out = open('pool_%s.log'%os.getpid(),'w')
                      sys.stderr = log_out
                      sys.stdout = log_out
                      log_out.write("Spawned")
                      log_out.flush()
                      log_out.write(" Complete
                  ")
                      log_out.flush()
                  
                  
                  def thread_dowork():
                      log_out.write("Entered function
                  ")
                      log_out.flush()
                      #Do Work
                      log_out.write("Exiting ")
                      log_out.flush()
                      log_out.close()
                      sys.exit(0)
                  

                  所有 12 个孩子的日志文件的输出是:

                  The output of the logfiles for all 12 children is:

                  Spawned
                  Complete
                  Entered function
                  Exiting
                  

                  主线程打印等待作业终止",然后就坐在那里.

                  The main thread prints 'Waiting for jobs to terminate', and then just sits there.

                  top 仅显示脚本的一份副本(我相信是主要的).htop 显示两个副本,一个是从顶部开始的副本,另一个是其他副本.根据它的 PID,它也不是孩子.

                  top shows only one copy of the script (the main one I believe). htop shows two copies, one of which is the one from top, and the other one of which is something else. Based on its PID, it's none of the children either.

                  有人知道我不知道的事情吗?

                  Does anyone know something I don't?

                  推荐答案

                  我真的没有答案,但我阅读了 Apply_async 的文档,这似乎与您所说的问题背道而驰......

                  I don't really have an answer but I read the docs for Apply_async and it seems counter to your stated problem...

                  回调应该立即完成,否则线程处理结果将被阻止.

                  Callbacks should complete immediately since otherwise the thread which handles the results will get blocked.

                  我不熟悉池,但在我看来,您的用例可以通过 本周 Python 模块

                  I'm not familiar with the Pool but it seems to me that your use-case could easily be handled by this recipe on Python Module of the Week

                  这篇关于Python 多处理从不加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:关于使用 Queue()/deque() 和类变量进行通信和“毒丸 下一篇:python multiprocessing BaseManager注册类在Ctrl-C后立即失

                  相关文章

                  最新文章

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

                    1. <tfoot id='5aoRD'></tfoot>

                        <bdo id='5aoRD'></bdo><ul id='5aoRD'></ul>

                      <small id='5aoRD'></small><noframes id='5aoRD'>