[英]How to wait for a spawned thread to finish in Python
I want to use threads to do some blocking work. 我想使用线程来做一些阻塞工作。 What should I do to: 我该怎么做:
Here is my code: 这是我的代码:
import threading
def my_thread(self):
# Wait for the server to respond..
def main():
a = threading.thread(target=my_thread)
a.start()
# Do other stuff here
You can use Thread.join
. 您可以使用Thread.join
。 Few lines from docs. 来自docs的几行。
Wait until the thread terminates. 等到线程终止。 This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs. 这会阻塞调用线程,直到调用join()方法的线程终止 - 正常或通过未处理的异常 - 或直到发生可选的超时。
For your example it will be like. 对于你的例子,它就像。
def main():
a = threading.thread(target = my_thread)
a.start()
a.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.