summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing/popen.py
blob: b0c80d5d87c863c2b04c9d6cae496f684fae144f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import threading

__all__ = ['Popen', 'get_spawning_popen', 'set_spawning_popen',
           'assert_spawning']

#
# Check that the current thread is spawning a child process
#

_tls = threading.local()

def get_spawning_popen():
    return getattr(_tls, 'spawning_popen', None)

def set_spawning_popen(popen):
    _tls.spawning_popen = popen

def assert_spawning(obj):
    if get_spawning_popen() is None:
        raise RuntimeError(
            '%s objects should only be shared between processes'
            ' through inheritance' % type(obj).__name__
            )

#
#
#

_Popen = None

def Popen(process_obj):
    if _Popen is None:
        set_start_method()
    return _Popen(process_obj)

def get_start_method():
    if _Popen is None:
        set_start_method()
    return _Popen.method

def set_start_method(meth=None, *, start_helpers=True):
    global _Popen
    try:
        modname = _method_to_module[meth]
        __import__(modname)
    except (KeyError, ImportError):
        raise ValueError('could not use start method %r' % meth)
    module = sys.modules[modname]
    if start_helpers:
        module.Popen.ensure_helpers_running()
    _Popen = module.Popen


if sys.platform == 'win32':

    _method_to_module = {
        None: 'multiprocessing.popen_spawn_win32',
        'spawn': 'multiprocessing.popen_spawn_win32',
        }

    def get_all_start_methods():
        return ['spawn']

else:
    _method_to_module = {
        None: 'multiprocessing.popen_fork',
        'fork': 'multiprocessing.popen_fork',
        'spawn': 'multiprocessing.popen_spawn_posix',
        'forkserver': 'multiprocessing.popen_forkserver',
        }

    def get_all_start_methods():
        from . import reduction
        if reduction.HAVE_SEND_HANDLE:
            return ['fork', 'spawn', 'forkserver']
        else:
            return ['fork', 'spawn']