本文介绍了Python - queue.task_done() 用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,该脚本具有多个线程(使用 threading.Thread 创建)使用 queue.get_nowait()Queue 获取 URL,然后处理 HTML.我是多线程编程的新手,无法理解 queue.task_done() 函数的用途.

I wrote a script that has multiple threads (created with threading.Thread) fetching URLs from a Queue using queue.get_nowait(), and then processing the HTML. I am new to multi-threaded programming, and am having trouble understanding the purpose of the queue.task_done() function.

Queue为空时,自动返回queue.Empty异常.所以我不明白每个线程需要调用 task_done() 函数.我们知道当队列为空时我们已经完成了队列,那么为什么我们需要通知它工作线程已经完成了他们的工作(这与队列无关,在他们从中获取了 URL 之后)?

When the Queue is empty, it automatically returns the queue.Empty exception. So I don't understand the need for each thread to call the task_done() function. We know that we're done with the queue when its empty, so why do we need to notify it that the worker threads have finished their work (which has nothing to do with the queue, after they've gotten the URL from it)?

谁能给我提供一个代码示例(最好使用 urllib、文件 I/O 或斐波那契数字以外的其他东西并打印Hello"),向我展示如何在实际应用?

Could someone provide me with a code example (ideally using urllib, file I/O, or something other than fibonacci numbers and printing "Hello") that shows me how this function would be used in practical applications?

推荐答案

Queue.task_done 不是为了工人的利益.支持Queue.join.

Queue.task_done is not there for the workers' benefit. It is there to support Queue.join.

如果我给你一盒作业,我会在乎你什么时候把所有东西都拿出来了吗?

If I give you a box of work assignments, do I care about when you've taken everything out of the box?

没有.我关心工作何时完成.看着一个空盒子并不能告诉我这一点.您和其他 5 个人可能仍在研究您开箱即用的东西.

No. I care about when the work is done. Looking at an empty box doesn't tell me that. You and 5 other guys might still be working on stuff you took out of the box.

Queue.task_done 让工作人员可以在 任务完成时发出通知.等待使用 Queue.join 完成所有工作的人将等到完成足够的 task_done 调用,而不是在队列为空时.

Queue.task_done lets workers say when a task is done. Someone waiting for all the work to be done with Queue.join will wait until enough task_done calls have been made, not when the queue is empty.

这篇关于Python - queue.task_done() 用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:33