My consumer side of the queue:
m = queue.get() queue.task_done() <rest of the program>
Questions:
Does task_done()
effectively pops m
off the queue and release whatever locks the consumer has on the queue?
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
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.
Yep, queue. get() will block only a thread where it was called.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With