为什么孩子们没死? [英] Why are the children failing to die?

查看:55
本文介绍了为什么孩子们没死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 terminate()方法可以杀死两个进程:

I expected the terminate() method to kill the two processes:

import multiprocessing
import time

def foo():
    while True:
        time.sleep(1)

def bar():
    while True:
        time.sleep(1)

if __name__ == '__main__':
    while True:
        p_foo = multiprocessing.Process(target=foo, name='foo')
        p_bar = multiprocessing.Process(target=bar, name='bar')
        p_foo.start()
        p_bar.start()
        time.sleep(1)
        p_foo.terminate()
        p_bar.terminate()
        print p_foo
        print p_bar

运行代码会给出:

<Process(foo, started)>
<Process(bar, started)>
<Process(foo, started)>
<Process(bar, started)>
...

我期待中:

<Process(foo, stopped)>
<Process(bar, stopped)>
<Process(foo, stopped)>
<Process(bar, stopped)>
...

推荐答案

因为终止函数只是发送SIGTERM信号进行处理,但是信号是异步的,因此您可以睡一段时间,或者等待处理终止(信号接收).

Because terminate function just send SIGTERM signal to process, but signals are asynchronous, so you can sleep for some time, or wait for processes termination(signal receiving).

例如,如果在终止后添加字符串 time.sleep(.1),则它可能会终止.

For example if you add string time.sleep(.1) after termination, it probably will be terminated.

这篇关于为什么孩子们没死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆