Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.active_children()

processes

Returns the list of the current process's children that are still alive. As a side effect it reaps children that have already finished, so they do not linger as zombies.

Syntax

multiprocessing.active_children()

Only direct children started through multiprocessing in this process are listed; grandchildren and subprocess processes are not. The side effect is useful: the call joins finished children, so long-running programs sometimes call it in a loop. A common shutdown idiom is to walk active_children(), call terminate() and join() so that no process is left hanging.

Examples

import multiprocessing as mp
def wait_for(ev):
    ev.wait(10)
if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    ev = ctx.Event()
    ps = [ctx.Process(target=wait_for, args=(ev,)) for _ in range(2)]
    for p in ps:
        p.start()
    print(len(mp.active_children()))  # → 2
    ev.set()
    for p in ps:
        p.join()
    print(mp.active_children())  # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy