[英]Does the return statement of the function which the thread executes automatically kill the thread?
After function2
executes and is returned, would the thread be killed automatically and do I need to join()
the thread or do something to make sure that it is completely gone in function1? 在
function2
执行并返回之后,该线程会自动被杀死,我是否需要join()
线程或执行某些操作以确保它完全不在function1中使用?
def function1():
a = 1
b = 2
Thread(target=function2, args=(a,b,)).start()
def function2(a, b):
print(a + b)
return()
No. The thread will terminate on its own. 否。线程将自行终止。 The
join()
function is a convenience that tells the parent thread to block until the child thread is finished (so if the child thread hasn't finished yet, then the parent thread won't continue). join()
函数是一种方便的方法,它告诉父线程在子线程完成之前阻塞(因此,如果子线程尚未完成,则父线程将不会继续执行)。
You can see evidence of this on the python console (even before I call t.join()
, the thread is already dead): 您可以在python控制台上看到这一点的证据(甚至在我调用
t.join()
,线程已经死了):
>>> def function1():
... print("Hello!")
... return 5
...
>>> t = threading.Thread(target=function1)
>>> t.start()
Hello!
>>> t.is_alive()
False
>>> t.join()
>>> t.is_alive()
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.