Python 线程:join()永远不会返回
我正在尝试创建多个线程,将它们添加到一个列表中,遍历该列表,然后像这样加入每个线程:Python 线程:join()永远不会返回,python,multithreading,Python,Multithreading,我正在尝试创建多个线程,将它们添加到一个列表中,遍历该列表,然后像这样加入每个线程: 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)
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")
它将打印“In-thread”和线程,但它从不打印“after-join”,因此我的所有代码after都不会运行
Runnable()
是我创建的一个函数,它也会打印它应该打印的内容,因此我不确定该代码是否与它有任何关系。在创建每个线程实例时调用Runnable
,因此线程的目标函数是它返回的任何内容(可能无)。尝试使用:
new_thread = threading.Thread(target=Runnable)
它具有target=Runnable
而不是target=Runnable()。尝试使用:
new_thread = threading.Thread(target=Runnable)
哪个函数的target=Runnable
而不是target=Runnable()
您的Runnable()函数是否返回过?否。它执行一些操作,然后向队列中添加一条记录,我稍后使用。这并不意味着返回任何内容。请考虑添加runnable
代码。runnable
是否曾经结束过?是创建一个要运行的对象,还是它只是开始做一些事情?(整个Runnable
听起来像是一个不必要的Java设计。)您的Runnable()函数是否返回过?否。它做了一些事情,然后向队列中添加一条记录,稍后我会使用它。这并不意味着返回任何内容。请考虑添加runnable
代码。runnable
是否曾经结束过?是创建一个要运行的对象,还是它只是开始做一些事情?(整个Runnable
听起来像是一个不必要的Java设计。)