multiprocessing: queue.get() blocks even if queue.qsize() != 0

R

redbaron

I run into problem with queue from multiprocessing. Even if I
queue.qsize() != 0 queue.get() still blocks and queue.get_nowait()
raises Emtpy error.

I'm unable to cut my big part to small test case, because smaller test
case similair to my real app by design is works. In what conditions is
it possible?

while qresult.qsize():
result = qresult.get() #this code blocks!
doWithResult(result)
 
M

MRAB

I run into problem with queue from multiprocessing. Even if I
queue.qsize() != 0 queue.get() still blocks and queue.get_nowait()
raises Emtpy error.

I'm unable to cut my big part to small test case, because smaller test
case similair to my real app by design is works. In what conditions is
it possible?

while qresult.qsize():
    result = qresult.get()  #this code blocks!
    doWithResult(result)

From Python v2.5 onwards queues also have a task_done() method. Try:

while qresult.qsize():
result = qresult.get() #this code blocks!
doWithResult(result)
qresult.task_done()
 
A

Antoon Pardon

I run into problem with queue from multiprocessing. Even if I
queue.qsize() != 0 queue.get() still blocks and queue.get_nowait()
raises Emtpy error.

I'm unable to cut my big part to small test case, because smaller test
case similair to my real app by design is works. In what conditions is
it possible?

while qresult.qsize():
result = qresult.get() #this code blocks!
doWithResult(result)

If you have more than one consumer the above code can block.
The two consumers both see that there is an item present
in the queue. One removes the item and the second blocks.
 
P

Paul Rubin

redbaron said:
while qresult.qsize():
result = qresult.get() #this code blocks!
doWithResult(result)

That is unreliable for the reason Antoon explained, and as is
documented in the manual for the Queue module. Write instead
something like (untested):

while True:
try:
result = qresult.get_nowait()
except Empty:
break
doWithResult(result)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top