在測試生產消費者模型的時候遇到這樣一個問題,在繼承線程後 加了個標誌位 mark
class Consumer(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self._queue = queue
self.mark = True
def run(self):
while self.mark:
msg = self._queue.get()
if isinstance(msg, str) and msg == 'quit':
break
print("I'm a thread, and I received %s!!" % msg)
print('Bye byes!')
def producer():
q = queue.Queue()
worker = Consumer(q)
worker.start() # 开启消费者线程
start_time = time.time()
while time.time() - start_time < 5:
q.put('something at %s' % time.time())
time.sleep(1)
worker.mark = Flese
worker.join()
我原本指望 透過這個標誌位元來實現執行緒的結束控制,但實際效果是程式卡死在worker.join()
完全沒有退出。
請教下,這是什麼原因?