multiprocessing.Pool结束进程的内部机制是什么?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论