Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Queue get()/task_done() issue

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

like image 542
bandana Avatar asked Oct 20 '09 08:10

bandana


People also ask

What is queue 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.

Is Python queue get blocking?

Yep, queue. get() will block only a thread where it was called.

How do I find the size of a python queue?

To get the length of a queue in Python:Use the len() function to get the length of a deque object. Use the qsize() method to get the length of a queue object.

Is queue in python thread-safe?

Thread Programming Luckily, Queue() class has a thread-safe implementation with all the required locking mechanism. So producer and consumer from different threads can work with the same queue instance safely and easily.


1 Answers

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.

like image 192
Lukáš Lalinský Avatar answered Oct 09 '22 12:10

Lukáš Lalinský