如何通过发信号的方式杀死父进程及其子进程?(进程.杀死.其子.方式.发信号...)

wufei1232025-03-16python873

如何通过发信号的方式杀死父进程及其子进程?

在多进程应用中,安全高效地终止父进程及其所有子进程至关重要。本文将探讨一种利用进程组和信号处理机制的优雅方法,避免子进程“孤儿”进程的产生。

传统方法使用os.kill只能单独终止进程,无法保证子进程也一同结束。为了解决这个问题,我们需要利用进程组的概念。进程组允许我们将父进程及其所有子进程组织在一起,通过向进程组发送信号来实现批量终止。

以下示例代码演示了如何通过进程组ID发送信号来终止父进程和子进程:

a.py (父进程):

import multiprocessing
import os
import signal
import time

def child_process():
    while True:
        print("子进程运行中...")
        time.sleep(1)

def signal_handler(signum, frame):
    print(f"父进程收到信号 {signum}")
    #  添加必要的清理工作

if __name__ == "__main__":
    child = multiprocessing.Process(target=child_process)
    child.daemon = True  # 设置为守护进程,父进程退出时子进程也退出
    child.start()

    pgid = os.getpgid(0) # 获取当前进程组ID
    with open("./pidfile", "w") as f:
        f.write(str(pgid))

    signal.signal(signal.SIGTERM, signal_handler)

    print("父进程运行中... PID:", os.getpid(), "进程组ID:", pgid)
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("父进程收到中断信号,准备退出...")
    finally:
        #  可选:添加额外的清理操作
        if os.path.exists("./pidfile"):
            os.remove("./pidfile")

b.py (发送信号):

import os
import signal

try:
    with open("./pidfile", "r") as f:
        pgid = int(f.read())
        os.killpg(pgid, signal.SIGTERM)
        print(f"已向进程组 {pgid} 发送SIGTERM信号")
except FileNotFoundError:
    print("pidfile 未找到")
except Exception as e:
    print(f"发送信号失败: {e}")

改进说明:

  • 使用os.getpgid(0): 获取当前进程的进程组ID,更简洁高效。
  • 守护进程: 将子进程设置为守护进程(child.daemon = True),父进程退出时,子进程会自动结束,无需额外处理。
  • try...except...finally: 确保在任何情况下都能执行必要的清理工作,例如删除pid文件。
  • 更清晰的错误处理: 完善的异常处理机制,提高代码的健壮性。

通过以上改进,代码更加简洁、安全可靠,有效地解决了父进程和子进程的终止问题,避免了资源泄露等问题。 记住在运行前创建a.py和b.py文件,并运行a.py启动父进程和子进程,然后运行b.py发送信号终止它们。 运行b.py后,记得手动删除./pidfile文件。

以上就是如何通过发信号的方式杀死父进程及其子进程?的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。