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

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

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

        Python 线程和队列示例

        时间:2023-09-29
          <bdo id='DgpnO'></bdo><ul id='DgpnO'></ul>

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

                • <tfoot id='DgpnO'></tfoot>

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

                • 本文介绍了Python 线程和队列示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 python 新手(我来自 PHP),我一直在阅读教程并尝试了几天,但我无法理解这个队列示例(http://docs.python.org/2/library/queue.html)

                  I'm new to python (I come from PHP), I've been reading tutorials and trying things for a couple of days but I can't understand this queue example (http://docs.python.org/2/library/queue.html)

                  def worker():
                      while True:
                          item = q.get()
                          do_work(item)
                          q.task_done()
                  
                  q = Queue()
                  for i in range(num_worker_threads):
                       t = Thread(target=worker)
                       t.daemon = True
                       t.start()
                  
                  for item in source():
                      q.put(item)
                  
                  q.join()       # block until all tasks are done
                  

                  我不明白的是工作线程是如何完成和存在的.我已经阅读了 q.get() 阻塞,直到一个项目可用,所以如果所有项目都已处理并且队列中没有任何项目,为什么 q.get() 不会永远阻塞?

                  The thing I don't understand is how the worker thread completes and exists. I've read q.get() blocks until an item is available, so if all items are processed and none is left in the queue why q.get() doesn't block forever?

                  推荐答案

                  这段代码中线程没有正常退出(确实是队列为空时阻塞).程序不会等待它们,因为它们是 守护线程.

                  Threads do not exit normally in this code (they are indeed blocked when the queue is empty). The program doesn't wait for them because they're daemon threads.

                  程序不会立即退出,也不会因为 q.joinq.task_done 调用.

                  The program doesn't exit immediately and doesn't block forever because of q.join and q.task_done calls.

                  每当将项目添加到队列中时,未完成任务的计数就会增加.每当消费者线程调用 task_done() 以指示该项目已被检索并且所有工作都已完成时,计数就会下降.当未完成任务的计数降至零时,join() 解除阻塞,程序无需等待守护线程即可存在.

                  The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks, and the program exists without waiting for daemon threads.

                  这篇关于Python 线程和队列示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 python 中正确使用多线程队列? 下一篇:如何在 python 中为 multiprocessing.Queue 实现 LIFO?

                  相关文章

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

                • <legend id='AR64R'><style id='AR64R'><dir id='AR64R'><q id='AR64R'></q></dir></style></legend>

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