Python 队列 get()/task_done() 问题
问题描述
我的消费者端:
m = queue.get()
queue.task_done()
<rest of the program>
问题:
task_done()
是否有效地将m
从队列中弹出并释放消费者在队列中的所有锁?
Does
task_done()
effectively popsm
off the queue and release whatever locks the consumer has on the queue?
我需要在程序的其余部分使用 m
.它是否安全,或者我需要在调用 task_done()
之前复制它还是 m
在 task_done()
之后可用?
I need to use m
during the rest of the program. Is it safe, or do I need to copy it before I call task_done()
or is m
usable after task_done()
?
快乐
解决方案
不,queue.get()
将项目从队列中弹出.在你这样做之后,你可以对它做任何你想做的事情,只要制作者按照它应该的方式工作并且不再触摸它.queue.task_done()
被调用只是为了通知队列你已经完成了某事(它甚至不知道具体的项目,它只是计算队列中未完成的项目),所以 queue.join()
知道工作已经完成.
No, queue.get()
pops the item off the queue. After you do that, you can do whatever you want with it, as long as the producer works like it should and doesn't touch it anymore. queue.task_done()
is called only to notify the queue that you are done with something (it doesn't even know about the specific item, it just counts unfinished items in the queue), so that queue.join()
knows the work is finished.
相关文章