I'm writing a multi-process program and using a queue to collect the results. After that, I move the results from the queue to a list in the main process (other processes are terminated). Here's the code
import multiprocessing
result = multiprocessing.Queue(maxsize=16)
# the multiprocessing part which fills the Queue with 16 elements
res_list = []
while not result.empty():
res_list.append(result.get())
print(results.qsize())
print(len(res_list))
I'm expecting
0
16
But the output is
3
13
I also got other numbers such as 4, 12 when running the program several times. Sometimes I do get 0, 16, but this is not guaranteed.
I've read this post: Multiprocessing Queue empty() function not working reliably in python
The answer mentioned that the number that qsize() returns might not be reliable. But I think it is only the number that is not reliable, the queue should actually be emptied. Also, empty() also returns true (so the program jumps out of the while part), so the queue should already be emptied.
However, the res_list only gets 13 elements. So I'm doubting if the queue is really emptied and why this happened. Also, does anyone have any idea about how can I get all my results from the result queue.
Thanks a lot!