Python 为什么Queue().get()可以停止(或中断)while true循环?
我正在学习线程和队列,我发现Queue().get()可以停止一个while循环。但我不知道为什么Python 为什么Queue().get()可以停止(或中断)while true循环?,python,multithreading,queue,Python,Multithreading,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
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
作为第一个参数传递。将确切告诉您原因Queue.get()
阻塞直到项目可用,除非您将False
作为第一个参数传递。如果您将False
传递到Queue.get
且队列为空Queue.empty
将被提升,因此,请做好捕获异常的准备。如果将False
传递给Queue.get
且队列为空Queue.empty
将被引发,请做好捕获异常的准备。