<tfoot id='X65q0'></tfoot>

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

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

      1. <legend id='X65q0'><style id='X65q0'><dir id='X65q0'><q id='X65q0'></q></dir></style></legend>

        将 multiprocessing.Queue 转储到列表中

        时间:2023-05-25
      2. <small id='3z6Xk'></small><noframes id='3z6Xk'>

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

            • <legend id='3z6Xk'><style id='3z6Xk'><dir id='3z6Xk'><q id='3z6Xk'></q></dir></style></legend>

                  <tbody id='3z6Xk'></tbody>

                1. <tfoot id='3z6Xk'></tfoot>
                2. 本文介绍了将 multiprocessing.Queue 转储到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我希望将 multiprocessing.Queue 转储到列表中.对于该任务,我编写了以下函数:

                  I wish to dump a multiprocessing.Queue into a list. For that task I've written the following function:

                  import Queue
                  
                  def dump_queue(queue):
                      """
                      Empties all pending items in a queue and returns them in a list.
                      """
                      result = []
                  
                      # START DEBUG CODE
                      initial_size = queue.qsize()
                      print("Queue has %s items initially." % initial_size)
                      # END DEBUG CODE
                  
                      while True:
                          try:
                              thing = queue.get(block=False)
                              result.append(thing)
                          except Queue.Empty:
                  
                              # START DEBUG CODE
                              current_size = queue.qsize()
                              total_size = current_size + len(result)
                              print("Dumping complete:")
                              if current_size == initial_size:
                                  print("No items were added to the queue.")
                              else:
                                  print("%s items were added to the queue." % 
                                        (total_size - initial_size))
                              print("Extracted %s items from the queue, queue has %s items 
                              left" % (len(result), current_size))
                              # END DEBUG CODE
                  
                              return result
                  

                  但由于某种原因它不起作用.

                  But for some reason it doesn't work.

                  观察以下 shell 会话:

                  Observe the following shell session:

                  >>> import multiprocessing
                  >>> q = multiprocessing.Queue()
                  >>> for i in range(100):
                  ...     q.put([range(200) for j in range(100)])
                  ... 
                  >>> q.qsize()
                  100
                  >>> l=dump_queue(q)
                  Queue has 100 items initially.
                  Dumping complete:
                  0 items were added to the queue.
                  Extracted 1 items from the queue, queue has 99 items left
                  >>> l=dump_queue(q)
                  Queue has 99 items initially.
                  Dumping complete:
                  0 items were added to the queue.
                  Extracted 3 items from the queue, queue has 96 items left
                  >>> l=dump_queue(q)
                  Queue has 96 items initially.
                  Dumping complete:
                  0 items were added to the queue.
                  Extracted 1 items from the queue, queue has 95 items left
                  >>> 
                  

                  这里发生了什么?为什么不是所有的物品都被倾倒了?

                  What's happening here? Why aren't all the items being dumped?

                  推荐答案

                  试试这个:

                  import Queue
                  import time
                  
                  def dump_queue(queue):
                      """
                      Empties all pending items in a queue and returns them in a list.
                      """
                      result = []
                  
                      for i in iter(queue.get, 'STOP'):
                          result.append(i)
                      time.sleep(.1)
                      return result
                  
                  import multiprocessing
                  q = multiprocessing.Queue()
                  for i in range(100):
                      q.put([range(200) for j in range(100)])
                  q.put('STOP')
                  l=dump_queue(q)
                  print len(l)
                  

                  多处理队列有一个内部缓冲区,该缓冲区有一个馈线线程,该线程从缓冲区中提取工作并将其刷新到管道中.如果不是所有的对象都被刷新,我可以看到 Empty 过早引发的情况.使用哨兵来指示队列的结束是安全的(可靠的).此外,使用 iter(get, sentinel) 习惯用法比依赖 Empty 更好.

                  Multiprocessing queues have an internal buffer which has a feeder thread which pulls work off a buffer and flushes it to the pipe. If not all of the objects have been flushed, I could see a case where Empty is raised prematurely. Using a sentinel to indicate the end of the queue is safe (and reliable). Also, using the iter(get, sentinel) idiom is just better than relying on Empty.

                  我不喜欢它可能由于刷新时间而升空(我添加了 time.sleep(.1) 以允许上下文切换到馈线线程,您可能不需要它,没有它它也可以工作 - 它是释放 GIL 的习惯).

                  I don't like that it could raise empty due to flushing timing (I added the time.sleep(.1) to allow a context switch to the feeder thread, you may not need it, it works without it - it's a habit to release the GIL).

                  这篇关于将 multiprocessing.Queue 转储到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python多处理:限制使用的核心数量 下一篇:使用多处理池的 apply_async 方法时谁运行回调?

                  相关文章

                  最新文章

                  <legend id='6d1rA'><style id='6d1rA'><dir id='6d1rA'><q id='6d1rA'></q></dir></style></legend>

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

                    <tfoot id='6d1rA'></tfoot>

                        <bdo id='6d1rA'></bdo><ul id='6d1rA'></ul>

                      <small id='6d1rA'></small><noframes id='6d1rA'>