35

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

simhumileco
  • 31,877
  • 16
  • 137
  • 115
bandana
  • 3,422
  • 6
  • 26
  • 30

1 Answers1

59

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.

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
  • 2
    does `task_done()` affect `qsize()`? That is, if the Queue has a size limit, when does it consider the slot "empty", after `get()` or after `task_done()`? – Shai Jul 12 '17 at 11:21
  • 5
    The slot is freed up after `get()`, `task_done()` is just an utility above the queue level, it does not really work with the queue itself. – Lukáš Lalinský Jul 29 '17 at 20:27