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

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

      1. <legend id='q1aGp'><style id='q1aGp'><dir id='q1aGp'><q id='q1aGp'></q></dir></style></legend>
        • <bdo id='q1aGp'></bdo><ul id='q1aGp'></ul>
        <tfoot id='q1aGp'></tfoot>
      2. 如何将队列引用传递给 pool.map_async() 管理的函数

        时间:2023-05-27
        <legend id='AcDmO'><style id='AcDmO'><dir id='AcDmO'><q id='AcDmO'></q></dir></style></legend>

      3. <small id='AcDmO'></small><noframes id='AcDmO'>

            <tbody id='AcDmO'></tbody>
          • <bdo id='AcDmO'></bdo><ul id='AcDmO'></ul>

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

                  本文介绍了如何将队列引用传递给 pool.map_async() 管理的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想要一个长时间运行的进程通过队列(或类似的东西)返回它的进度,我将把它提供给进度条对话框.当过程完成时,我还需要结果.此处的测试示例失败并出现 RuntimeError: Queue objects should only be shared between processes through inheritance.

                  I want a long-running process to return its progress over a Queue (or something similar) which I will feed to a progress bar dialog. I also need the result when the process is completed. A test example here fails with a RuntimeError: Queue objects should only be shared between processes through inheritance.

                  import multiprocessing, time
                  
                  def task(args):
                      count = args[0]
                      queue = args[1]
                      for i in xrange(count):
                          queue.put("%d mississippi" % i)
                      return "Done"
                  
                  def main():
                      q = multiprocessing.Queue()
                      pool = multiprocessing.Pool()
                      result = pool.map_async(task, [(x, q) for x in range(10)])
                      time.sleep(1)
                      while not q.empty():
                          print q.get()
                      print result.get()
                  
                  if __name__ == "__main__":
                      main()
                  

                  我已经能够使用单独的 Process 对象(我 am 允许传递 Queue 引用)使其工作,但是我没有一个池来管理我的许多进程想启动.有什么更好的模式建议吗?

                  I've been able to get this to work using individual Process objects (where I am alowed to pass a Queue reference) but then I don't have a pool to manage the many processes I want to launch. Any advise on a better pattern for this?

                  推荐答案

                  以下代码似乎可以工作:

                  The following code seems to work:

                  import multiprocessing, time
                  
                  def task(args):
                      count = args[0]
                      queue = args[1]
                      for i in xrange(count):
                          queue.put("%d mississippi" % i)
                      return "Done"
                  
                  
                  def main():
                      manager = multiprocessing.Manager()
                      q = manager.Queue()
                      pool = multiprocessing.Pool()
                      result = pool.map_async(task, [(x, q) for x in range(10)])
                      time.sleep(1)
                      while not q.empty():
                          print q.get()
                      print result.get()
                  
                  if __name__ == "__main__":
                      main()
                  

                  请注意,队列来自 manager.Queue() 而不是 multiprocessing.Queue().感谢 Alex 为我指明了这个方向.

                  Note that the Queue is got from a manager.Queue() rather than multiprocessing.Queue(). Thanks Alex for pointing me in this direction.

                  这篇关于如何将队列引用传递给 pool.map_async() 管理的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:与多处理错误的另一个混淆,“模块"对象没 下一篇:Python 多进程池.当其中一个工作进程确定不再需要

                  相关文章

                  最新文章

                1. <legend id='yzbN2'><style id='yzbN2'><dir id='yzbN2'><q id='yzbN2'></q></dir></style></legend>
                  • <bdo id='yzbN2'></bdo><ul id='yzbN2'></ul>

                    <tfoot id='yzbN2'></tfoot>

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

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