【发布时间】:2021-05-23 23:43:45
【问题描述】:
基本上,我有一个程序,我想在其中创建一个线程来执行主线程旁边的某些功能。但是在python中似乎当我创建一个线程时,直到创建线程还没有完成,执行才不会传递给主线程。
请看下面的例子:
import threading
import time
def testing():
time.sleep(30)
wt = threading.Thread(target=testing, name="test", daemon=True)
print("Starting thread")
wt.start()
wt.join()
print("do other stuff next to above thread")
如果你运行上面的程序,直到测试功能没有完成,主程序不会打印do other stuff next to above thread。
不确定我是否缺少某些参数或什么,但有人可以告诉我如何创建一个线程让主程序继续执行吗?
【问题讨论】:
-
wt.join()明确等待wt结束。主线程 正在 并行执行,但它正在执行您告诉它执行的操作:等待wt结束。将wt.join()下移一行,主线程将立即打印"do other stuff ..."。
标签: python multithreading