线程:join() 永远不会返回



我正在尝试创建多个线程,将它们添加到列表中,浏览该列表并像这样join每个线程:

threads = []
for i in range(0, 4):
new_thread = threading.Thread(target=Runnable())
new_thread.start()
threads.append(new_thread)
for thread in threads:
print("in thread")
print(thread)
thread.join()
print("after join")
它将打印">

在线程中"和线程,但它永远不会打印"连接后",因此我之后的所有代码都没有运行。Runnable()是我创建的函数,它也打印了它应该打印的内容,所以我不确定该代码是否与它有关。

您在创建每个Thread实例时调用Runnable,因此线程的target函数是它返回的任何内容(可能是None(。尝试使用:

new_thread = threading.Thread(target=Runnable)

它有target=Runnable而不是target=Runnable().

最新更新