[issue5906] Risk of confusion in multiprocessing module - daemonic processes

Tim Peters report at bugs.python.org
Sat Apr 1 14:07:16 EDT 2017


Tim Peters added the comment:

I'll take a crack at these ancient comments ;-)

"""
daemon
    The process’s daemon flag, a Boolean value. This must be set before start() is called.
    The initial value is inherited from the creating process. [1]
    When a process exits, it attempts to terminate all of its daemonic child processes. [2]

[1] this sentence is weird: since daemonic processes are not allowed to have children, isn't this flag always False in a new Process instance ?
"""

Yes.  I agree it's confusing; looks like it was pasted from the `threading` docs a long time ago, in which context it had a non-degenerate meaning.

I've personally found this `multiprocessing` restriction (daemonic processes can't have children) to be nothing but a pain in the ass, especially since `Pool` worker processes are (although this doesn't appear to be documented) created as daemonic processes.  (Note that `concurrent.futures.ProcessPoolExecutor` doesn't have this restriction.)

"""
[2] typo, it meant "all of its NON-daemonic child processes" instead, didn't it ?
"""

No, this was correct.  When a process exits, it waits to join its non-daemonic children, but brutally terminates its daemonic children midstream.  For example, run this:

import multiprocessing as mp
from time import sleep

def e(tag):
    for i in range(10):
        print(tag, i)
        sleep(1)

if __name__ == '__main__':
    p = mp.Process(target=e, args=("daemon",), daemon=True)
    q = mp.Process(target=e, args=("normal",))
    p.start()
    q.start()
    sleep(5)

You'll see that, after 5 seconds, the main process exits and terminates the daemonic process immediately, but waits for the non-daemonic process to finish:

daemon 0
normal 0
daemon 1
normal 1
daemon 2
normal 2
daemon 3
normal 3
daemon 4
normal 4
normal 5
normal 6
normal 7
normal 8
normal 9

----------
nosy: +tim.peters

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5906>
_______________________________________


More information about the Python-bugs-list mailing list