Python Glossary Русский

Python 3.4+multiprocessingfunction

multiprocessing.get_start_method()

start methods

Returns the name of the process start method: 'spawn', 'fork' or 'forkserver'. With allow_none=True it returns None if no method has been chosen yet.

Syntax

multiprocessing.get_start_method(allow_none=False)

The trap: calling it without allow_none=True does not just read the value, it fixes the global default, and a later set_start_method() fails with RuntimeError "context has already been set". The default depends on the OS and version: Windows uses spawn, macOS spawn since 3.8, Linux fork up to 3.13 and forkserver since 3.14. That is why code tested on pre-3.14 Linux with fork breaks on macOS, Windows and newer Linux: the child no longer inherits the parent's memory, and everything has to go through pickle.

Examples

import multiprocessing as mp
if __name__ == '__main__':
    print(mp.get_start_method(allow_none=True))  # → None
    print(mp.get_start_method())  # → ? forkserver на Linux с 3.14, fork раньше, spawn на Windows и macOS
    try:
        mp.set_start_method('spawn')
    except RuntimeError as e:
        print(e)  # → context has already been set

See also

Open in the full glossarydocs.python.orgReport an inaccuracy