我不知道为什么下面的代码有效。我坚持的部分是
caMelCase
对我而言,所有这一切都是等待每个for t in worker_threads:
t.join()
主题转到worker
进入.put(1)
。
我不太确定counter_queue
这些行的目的是否会立即摧毁它们。我可能会忽略这些重复行的重要性:
del t
关于t = threading.Thread(target=print_manager)
t.daemon = True
t.start()
del t
旗帜,我有什么遗漏吗?或者还有别的吗?
.daemon
输出:
import threading, queue
###########################################################################################
counter = 0
counter_queue = queue.Queue()
def counter_manager():
'I have EXCLUSIVE rights to update the counter variable'
global counter
while True:
increment = counter_queue.get()
counter += increment
print_queue.put([
'The count is %d' % counter,
'---------------'])
counter_queue.task_done()
t = threading.Thread(target=counter_manager)
t.daemon = True
t.start()
del t
###########################################################################################
print_queue = queue.Queue()
def print_manager():
'I have EXCLUSIVE rights to call the "print" keyword'
while True:
job = print_queue.get()
for line in job:
print(line)
print_queue.task_done()
t = threading.Thread(target=print_manager)
t.daemon = True
t.start()
del t
###########################################################################################
def worker():
'My job is to increment the counter and print the current count'
counter_queue.put(1)
print_queue.put(['Starting up'])
worker_threads = []
for i in range(10):
t = threading.Thread(target=worker)
worker_threads.append(t)
t.start()
for t in worker_threads:
t.join()
counter_queue.join()
print_queue.put(['Finishing up'])
print_queue.join()
答案 0 :(得分:0)
所以事实证明我对del
的功能有误解。 del
实际上取消引用对象/线程。它仍然存在于后台,仍在运行并收听输入!