This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: multpirocessing.Process alive after SIGTERM on parent
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: davin, iritkatriel, lids, pitrou
Priority: normal Keywords:

Created on 2019-02-15 11:55 by lids, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg335601 - (view) Author: Defert (lids) Date: 2019-02-15 11:55
Hello,

Using the multiprocessing.Process class on Python 3.5 (untested with other versions), child processes are not killed when the main process is killed.

The doc mentions a "daemon" flag (https://python.readthedocs.io/en/latest/library/multiprocessing.html#multiprocessing.Process.daemon), which says "When a process exits, it attempts to terminate all of its daemonic child processes."

However this does not seem to be the case, when the parent process is killed, all children remain alive whatever the value of the daemon flag is.

Test code:

from multiprocessing import Process
from time import sleep
from os import getpid
 
def log(daemon_mode):
    while True:
        print('worker %i %s' % (getpid(), daemon_mode))
        sleep(3)
  
print('parent pid %i' % getpid())
a = Process(target=log, args=(0,), daemon=False)
a.start()
   
b = Process(target=log, args=(1,), daemon=True)
b.start()
   
while True:
    sleep(60)

######

To be run with:

user@host~# python3 test.py &
[1] 14749
parent pid 14749
worker 14751 1
worker 14750 0
user@host:~# 
user@host:~# kill 14749
[1]+  Terminated              python3 test.py
user@host:~#
worker 14751 1
worker 14750 0
msg408327 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-11 18:15
This example is not working for me on 3.11:

>>> from multiprocessing import Process
>>> from time import sleep
>>> from os import getpid
>>> 
>>> def log(daemon_mode):
...     while True:
...         print('worker %i %s' % (getpid(), daemon_mode))
...         sleep(3)
... 
>>> 
>>> print('parent pid %i' % getpid())
parent pid 77378
>>> 
>>> a = Process(target=log, args=(0,), daemon=False)
>>> a.start()
>>> <frozen importlib._bootstrap>:744: DeprecationWarning: BuiltinImporter.module_repr() is deprecated and slated for removal in Python 3.12
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython-654/Lib/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/iritkatriel/src/cpython-654/Lib/multiprocessing/spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'log' on <module '__main__' (built-in)>
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80180
2021-12-11 18:15:59iritkatrielsetnosy: + iritkatriel
messages: + msg408327
2019-02-17 09:02:48SilentGhostsetnosy: + pitrou, davin
type: behavior
2019-02-15 11:55:24lidscreate