[英]terminating a process within itself in python
假设我这样做:
from multiprocessing import Process
def a():
#Do something
x = Process(target = a)
x.start()
我想在定义的函数中使用x.terminate()
,但是要做到这一点,我需要添加x作为参数,同时定义x,这是不可能的。
所以我的问题是,如何在函数中终止进程?
刚刚return
从a
,而该过程将结束。 (通过在函数末尾运行来隐式返回也很重要。)
不过,您需要if __name__ == '__main__'
防护措施来安全地创建流程:
if __name__ == '__main__':
x = Process(target=a)
x.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.