Python 线程和队列示例

Python threads and queue example(Python 线程和队列示例)

本文介绍了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 线程和队列示例