Python 线程和队列示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15173975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python threads and queue example
提问by nacholibre
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)
我是 Python 新手(我来自 PHP),我已经阅读教程并尝试了几天,但我无法理解这个队列示例(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
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?
我不明白的是工作线程是如何完成和存在的。我已经阅读了 q.get() 阻塞直到一个项目可用,所以如果所有项目都被处理并且队列中没有任何项目,为什么 q.get() 不会永远阻塞?
采纳答案by Pavel Anossov
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.
线程在这段代码中没有正常退出(当队列为空时它们确实被阻塞了)。程序不会等待它们,因为它们是守护线程。
The program doesn't exit immediately and doesn't block forever because of q.joinand q.task_donecalls.
程序不会立即退出,也不会因为q.join和q.task_done调用而永远阻塞。
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.
每当将项目添加到队列时,未完成任务的计数就会增加。每当消费者线程调用task_done()以指示该项目已被检索并且其上的所有工作已完成时,计数就会下降。当未完成任务的计数降至零时,join()解除阻塞,程序存在而无需等待守护线程。
回答by Anton Danilchenko
I have had the same problem. When all threads completed, I saw "sleeping threads" in process list (use top -H -p <pid>where <pid>is process id from ps aux | grep pythonwith your script).
我曾经也有过一样的问题。当所有线程完成后,我在进程列表中看到了“休眠线程”(使用脚本中的进程 ID 来自top -H -p <pid>哪里)。<pid>ps aux | grep python
I solved this problem by replacing "infinite loop" while Trueto while not q.empty():.
我通过将“无限循环”替换while True为while not q.empty():.
It fixed the problem with "sleeping threads".
它解决了“休眠线程”的问题。
def worker():
while not q.empty():
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

