multiprocessing.Pool结束进程的内部机制是什么?

发布于 2025-01-11 13:07:34 字数 1504 浏览 0 评论 0原文

我对 multiprocessing.Pool 内部的一些机制很好奇。例如,我测试了以下代码:

from multiprocessing import Pool, current_process
from time import sleep


def proc():
    while True:
        print(f'{current_process()} is running...')
        sleep(5)


def main():
    pool = Pool(4)
    pool.apply_async(proc)
    pool.apply_async(proc)
    pool.close()
    pool.join()


if __name__ == '__main__':
    main()

当代码运行时,有五个进程。主进程的pid是71190。然后我打开另一个终端,运行 os.kill(71190, signal.SIGTERM) 来终止主进程。然后杀死三个进程,主进程和两个空闲进程,剩下两个进程。

然后我添加了一些代码:

from multiprocessing import Pool, current_process
from time import sleep
import signal


def handler(*args):
    print(f'{current_process()} is killing...')
    raise SystemExit(1)


signal.signal(signal.SIGTERM, handler)


def proc():
    while True:
        print(f'{current_process()} is running...')
        sleep(5)


def main():
    pool = Pool(4)
    pool.apply_async(proc)
    pool.apply_async(proc)
    pool.close()
    pool.join()


if __name__ == '__main__':
    main()

然后我再次运行 os.kill(74407, signal.SIGTERM) ,这次所有进程都被杀死。而输出是:

<_MainProcess(MainProcess, started)> is killing...
<ForkProcess(ForkPoolWorker-1, started daemon)> is killing...
<ForkProcess(ForkPoolWorker-2, started daemon)> is killing...

我很好奇发生了什么,我只是被杀死了主进程,为什么子进程也收到了信号?我以前是找出所有进程的PID,然后一一杀掉。我认为 Pool 一定在内部做了什么。谁能为我解释一下吗?非常感谢。

I was curious about some of the mechanics inside the multiprocessing.Pool.For example, I tested the following code:

from multiprocessing import Pool, current_process
from time import sleep


def proc():
    while True:
        print(f'{current_process()} is running...')
        sleep(5)


def main():
    pool = Pool(4)
    pool.apply_async(proc)
    pool.apply_async(proc)
    pool.close()
    pool.join()


if __name__ == '__main__':
    main()

When the code runs, there are five processes. The main process's pid is 71190. Then I turned on another terminal, run os.kill(71190, signal.SIGTERM) to kill the main process. Then killed three processes, main process and two idle processes, remaining two processes.

Then I added some code:

from multiprocessing import Pool, current_process
from time import sleep
import signal


def handler(*args):
    print(f'{current_process()} is killing...')
    raise SystemExit(1)


signal.signal(signal.SIGTERM, handler)


def proc():
    while True:
        print(f'{current_process()} is running...')
        sleep(5)


def main():
    pool = Pool(4)
    pool.apply_async(proc)
    pool.apply_async(proc)
    pool.close()
    pool.join()


if __name__ == '__main__':
    main()

Then I run os.kill(74407, signal.SIGTERM) again, This time all processes were killed. And the output is:

<_MainProcess(MainProcess, started)> is killing...
<ForkProcess(ForkPoolWorker-1, started daemon)> is killing...
<ForkProcess(ForkPoolWorker-2, started daemon)> is killing...

I'm very curious about what happened, I was only killed main process, why did the child process also receive the signal? I used to find out the PID of all processes, and then kill it one by one. I think Pool must have done something internally. Can anyone explain it for me? Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文