Reputation: 59
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
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