假设我有类似的代码:
from multiprocessing import Process
def pro_a():
#execute some code that runs a radio program
def pro_b():
#execute some code that keeps a GUI running
.
.
. #some code
if __name__ == '__main__':
Process(target=pro_a).start()
Process(target=pro_b).start()
这些进程开始后如何结束它们?我试过 .end()、.quit()、.terminate() 并且每次,命令窗口都会显示“进程”对象没有属性“...”(Linux)
在我的特定代码中,我以某个中心频率运行 GNU radio ,然后我希望我的 GUI 能够有一个按钮,当按下该按钮时,更新中心频率,然后关闭进程并重新运行它们,以便使用新的中心频率。
最佳答案
Comment: I need to terminate it ... from another process
您可以使用
os.kill(...
.os.kill(pid, sig)
Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.
例如:
os.kill(p.pid, signal.SIGTERM)
Note: Watch out the warnings about
kill
aprocess
in the Documentation.
Python » 3.6.1 Documentation multiprocessing.Process.terminate
如果你这样使用它,你会从
.start()
获得返回值那是None
:if __name__ == '__main__':
p =Process(target=pro_a).start()
例如,您必须这样做:
if __name__ == '__main__':
p= Process(target=pro_a)
p.start()
time.sleep(5)
p.terminate()
关于Python - 如何杀死一个进程(不是子进程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43923495/