0

I am trying to run a separate Python Process and store the result in the queue. I can extract the result in two ways: either run queue.get() just once or use a while loop and iterate over queue until it`s empty. In the code below first method is used if first=True and second method is used if first=False.

from multiprocessing import Process, Queue


def foo1(queue):
    queue.put(1)


def main(first=False):

    queue = Queue()

    p = Process(target=foo1, args=(queue,))
    p.start()

    if first:
        a = queue.get()
        print(a)
    else:
        while not queue.empty():
            print(queue.get())

    p.join()

if __name__ == "__main__":
    main()

Question: Why does first method print 1 correctly and second does not ? Aren`t they supposed to be equal ?

I am using Windows 10. I noticed this behavior in both interactive console and shell terminal.

Note: Due to the bug mentioned here I have to run the code as one script.

Kreol
  • 207
  • 2
  • 7
  • The queue is empty at the beginning, until `foo1` is executed, and that happens after a delay. So, you have no way of differentiating between "not filled yet" and "already emptied". – zvone Dec 29 '20 at 01:15
  • would you like to post as answer ? I ll accept – Kreol Dec 29 '20 at 01:31

0 Answers0