Python线程问题:线程相互阻塞

Python线程问题:线程相互阻塞,python,multithreading,Python,Multithreading,我正在尝试用python实现一个简单的线程池 我使用以下代码启动几个线程: threads = [] for i in range(10): t = threading.Thread(target=self.workerFuncSpinner( taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i)) t.setDaemon(True)

我正在尝试用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.Sleep之间打印。问题是,不是得到如下输出:

#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()时,它们都报告它们是主线程

这就像没有线程,而是在主线程上下文中运行

帮忙


谢谢

我想可能是你的问题。我将验证它是否实际正在运行任务,而是返回一个可调用对象以供线程运行


创建
线程
对象时,您正在主线程中调用
workerFuncSpinner
。改为使用对方法的引用:

t=threading.Thread(target=self.workerFuncSpinner, 
    args=(taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))
您的原始代码:

t = threading.Thread(target=self.workerFuncSpinner(
    taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))
t.start()
可以改写为

# call the method in the main thread
spinner = self.workerFuncSpinner(
    taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i)

# create a thread that will call whatever `self.workerFuncSpinner` returned, 
# with no arguments
t = threading.Thread(target=spinner)

# run whatever workerFuncSpinner returned in background thread
t.start()

您在主线程中串行调用该方法,而在创建的线程中没有调用任何方法。

就是这样。我不太明白为什么one-arg版本采用主线程上下文,但是谢谢。