Python队列get()/task_done()问题 [英] Python Queue get()/task_done() issue

查看:558
本文介绍了Python队列get()/task_done()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的消费方队列:

m = queue.get()
queue.task_done()

<rest of the program>

问题:

  1. task_done()是否有效地将m从队列中弹出并释放消费者在队列上的所有锁定?

  2. 在程序的其余部分中,我需要使用m.是安全的,还是我需要在调用task_done()之前复制它,或者mtask_done()之后可用?

开心

解决方案

否,queue.get()将项目从队列中弹出.完成此操作后,只要生产者按其应有的方式工作并且不再碰它,就可以随心所欲地对其进行操作.调用queue.task_done()仅是为了通知队列您已完成某件事(它甚至不知道特定项目,它只计算队列中未完成的项目),以便queue.join()知道工作已完成. /p>

My consumer side of the queue:

m = queue.get()
queue.task_done()

<rest of the program>

Questions:

  1. Does task_done() effectively pops m off the queue and release whatever locks the consumer has on the queue?

  2. 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()?

be happy

解决方案

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.

这篇关于Python队列get()/task_done()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆