Python - 如何杀死一个进程(不是子进程)

标签 python ubuntu process multiprocessing

假设我有类似的代码:

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 a process 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/

相关文章:

java - 如何将字符串变量作为 shell 脚本参数传递?

python - pandas 如何使用 groupby 在标签中按日期对列进行分组?

git - 如何恢复使用 `git rm .` 意外删除的文件?

python - 带有 Pylons 1.0 的 TurboMail 3 - MailNotEnabledException

linux - 为什么用qemu kvm 打开.qcow2 很慢?解决办法是什么?

ubuntu - 无法访问 Oracle Cloud Always Free Compute http 端口

java - 如何执行jar可执行文件并接收返回值

c++ - Windows 上的线程

python - 正则表达式匹配以分号分隔的标记序列的字符串

python - 使用基于条件的字典替换 python 数据框中的列值