0

I have a multiprocess program in which one process puts many items on a queue and then sets an event after it has finished putting items on the queue. Another process waits for that event and then empties the queue in a while loop as in the code below, checking with an empty() call. The problem is that every once in awhile (maybe 1 time out of 10), empty() returns True when there are still items on the queue. During the while loop, no other process or thread is touching the queue. The issue can be reproduced with the below code in a single process. Most of the time, the output is:

The size of my_queue initially is 400
The size of my_queue is 0

But every once in a while, I will get something like:

The size of my_queue initially is 400
The size of my_queue is 377

Here is the code:

from multiprocessing import Queue as MPQueue

my_list = []
my_queue = MPQueue()
for i in range(0, 400):
    my_queue.put(f"The value of i is {i}")

print(f"The size of my_queue initially is {my_queue.qsize()}")
while not my_queue.empty():
    my_list.append(my_queue.get())
print(f"The size of my_queue is {my_queue.qsize()}")

Is this a bug in the multiprocessing.Queue module or am I missing something? I've also tried using get_nowait() and then catching the Queue.Empty exception, but I get the same result. Any help would be much appreciated. Thanks.

1 Answer 1

1

I meet the same issue at some point. docs.python.org

class multiprocessing.Queue([maxsize])

qsize():

Return the approximate size of the queue. Because of > multithreading/multiprocessing semantics, this number is not reliable.

Note that this may raise NotImplementedError on Unix platforms like Mac OS X > where sem_getvalue() is not implemented.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.