【发布时间】:2015-04-28 19:39:23
【问题描述】:
我正在尝试在 python 中实现一个简单的线程池。
我用以下代码启动了几个线程:
threads = []
for i in range(10):
t = threading.Thread(target=self.workerFuncSpinner(
taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))
t.setDaemon(True)
threads.append(t)
t.start()
for thread in threads:
thread.join()
此时,工作线程仅在启动和退出以及 time.sleeps 之间进行打印。问题是,而不是得到像这样的输出:
#All output at the same time
thread 1 starting
thread 2 starting
thread n starting
# 5 seconds pass
thread 1 exiting
thread 2 exiting
thread n exiting
I get:
thread 1 starting
# 5 seconds pass
thread 1 exiting
thread 2 starting
# 5 seconds pass
thread 2 exiting
thread n starting
# 5 seconds pass
thread n exiting
当我执行 threading.current_thread() 时,它们都报告说它们是主线程。
就像没有线程,而是在主线程上下文中运行。
帮助?
谢谢
【问题讨论】:
标签: python multithreading