Anthony Lee
Anthony Lee

Reputation: 59

Why Queue().get() can stop(or break) a while true loop?

I am learning Thread and Queue, and I found that Queue().get() can stop a while loop. But I don't know why.

from threading import Thread
from queue import Queue

def working():
    while True:
        print("loop...")
        Queue().get()         ## why here ##

for i in range(5):    
    t = Thread(target=working)
    t.start()

If I remove "Queue().get()", it will become a infinite loop.

Upvotes: 2

Views: 4275

Answers (1)

Mike Corcoran
Mike Corcoran

Reputation: 14565

The documentation tells you exactly why. Queue.get() blocks until an item is available unless you pass False as the first parameter.

Upvotes: 4

Related Questions