Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the children failing to die?

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

Running the code gives:

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

I was expecting:

<Process(foo, stopped)>
<Process(bar, stopped)>
<Process(foo, stopped)>
<Process(bar, stopped)>
...
like image 550
tshepang Avatar asked Aug 13 '12 21:08

tshepang


1 Answers

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).

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

like image 154
Fedor Gogolev Avatar answered Nov 15 '22 15:11

Fedor Gogolev