为什么 Queue().get() 可以停止(或中断)一个真正的循环



我正在学习Thread and Queue,我发现Queue().get()可以停止一个while循环。但我不知道为什么。

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()

如果我删除"Queue().get()",它将成为一个无限循环。

文档确切地告诉您原因。 Queue.get()块,直到项目可用,除非您将False作为第一个参数传递。

最新更新