| 1 | n/a | import os |
|---|
| 2 | n/a | import sys |
|---|
| 3 | n/a | import threading |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | from . import process |
|---|
| 6 | n/a | from . import reduction |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | __all__ = [] # things are copied from here to __init__.py |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | # |
|---|
| 11 | n/a | # Exceptions |
|---|
| 12 | n/a | # |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | class ProcessError(Exception): |
|---|
| 15 | n/a | pass |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | class BufferTooShort(ProcessError): |
|---|
| 18 | n/a | pass |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | class TimeoutError(ProcessError): |
|---|
| 21 | n/a | pass |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | class AuthenticationError(ProcessError): |
|---|
| 24 | n/a | pass |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | # |
|---|
| 27 | n/a | # Base type for contexts |
|---|
| 28 | n/a | # |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | class BaseContext(object): |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | ProcessError = ProcessError |
|---|
| 33 | n/a | BufferTooShort = BufferTooShort |
|---|
| 34 | n/a | TimeoutError = TimeoutError |
|---|
| 35 | n/a | AuthenticationError = AuthenticationError |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | current_process = staticmethod(process.current_process) |
|---|
| 38 | n/a | active_children = staticmethod(process.active_children) |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def cpu_count(self): |
|---|
| 41 | n/a | '''Returns the number of CPUs in the system''' |
|---|
| 42 | n/a | num = os.cpu_count() |
|---|
| 43 | n/a | if num is None: |
|---|
| 44 | n/a | raise NotImplementedError('cannot determine number of cpus') |
|---|
| 45 | n/a | else: |
|---|
| 46 | n/a | return num |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def Manager(self): |
|---|
| 49 | n/a | '''Returns a manager associated with a running server process |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | The managers methods such as `Lock()`, `Condition()` and `Queue()` |
|---|
| 52 | n/a | can be used to create shared objects. |
|---|
| 53 | n/a | ''' |
|---|
| 54 | n/a | from .managers import SyncManager |
|---|
| 55 | n/a | m = SyncManager(ctx=self.get_context()) |
|---|
| 56 | n/a | m.start() |
|---|
| 57 | n/a | return m |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def Pipe(self, duplex=True): |
|---|
| 60 | n/a | '''Returns two connection object connected by a pipe''' |
|---|
| 61 | n/a | from .connection import Pipe |
|---|
| 62 | n/a | return Pipe(duplex) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def Lock(self): |
|---|
| 65 | n/a | '''Returns a non-recursive lock object''' |
|---|
| 66 | n/a | from .synchronize import Lock |
|---|
| 67 | n/a | return Lock(ctx=self.get_context()) |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def RLock(self): |
|---|
| 70 | n/a | '''Returns a recursive lock object''' |
|---|
| 71 | n/a | from .synchronize import RLock |
|---|
| 72 | n/a | return RLock(ctx=self.get_context()) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | def Condition(self, lock=None): |
|---|
| 75 | n/a | '''Returns a condition object''' |
|---|
| 76 | n/a | from .synchronize import Condition |
|---|
| 77 | n/a | return Condition(lock, ctx=self.get_context()) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def Semaphore(self, value=1): |
|---|
| 80 | n/a | '''Returns a semaphore object''' |
|---|
| 81 | n/a | from .synchronize import Semaphore |
|---|
| 82 | n/a | return Semaphore(value, ctx=self.get_context()) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def BoundedSemaphore(self, value=1): |
|---|
| 85 | n/a | '''Returns a bounded semaphore object''' |
|---|
| 86 | n/a | from .synchronize import BoundedSemaphore |
|---|
| 87 | n/a | return BoundedSemaphore(value, ctx=self.get_context()) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | def Event(self): |
|---|
| 90 | n/a | '''Returns an event object''' |
|---|
| 91 | n/a | from .synchronize import Event |
|---|
| 92 | n/a | return Event(ctx=self.get_context()) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | def Barrier(self, parties, action=None, timeout=None): |
|---|
| 95 | n/a | '''Returns a barrier object''' |
|---|
| 96 | n/a | from .synchronize import Barrier |
|---|
| 97 | n/a | return Barrier(parties, action, timeout, ctx=self.get_context()) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | def Queue(self, maxsize=0): |
|---|
| 100 | n/a | '''Returns a queue object''' |
|---|
| 101 | n/a | from .queues import Queue |
|---|
| 102 | n/a | return Queue(maxsize, ctx=self.get_context()) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def JoinableQueue(self, maxsize=0): |
|---|
| 105 | n/a | '''Returns a queue object''' |
|---|
| 106 | n/a | from .queues import JoinableQueue |
|---|
| 107 | n/a | return JoinableQueue(maxsize, ctx=self.get_context()) |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def SimpleQueue(self): |
|---|
| 110 | n/a | '''Returns a queue object''' |
|---|
| 111 | n/a | from .queues import SimpleQueue |
|---|
| 112 | n/a | return SimpleQueue(ctx=self.get_context()) |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | def Pool(self, processes=None, initializer=None, initargs=(), |
|---|
| 115 | n/a | maxtasksperchild=None): |
|---|
| 116 | n/a | '''Returns a process pool object''' |
|---|
| 117 | n/a | from .pool import Pool |
|---|
| 118 | n/a | return Pool(processes, initializer, initargs, maxtasksperchild, |
|---|
| 119 | n/a | context=self.get_context()) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def RawValue(self, typecode_or_type, *args): |
|---|
| 122 | n/a | '''Returns a shared object''' |
|---|
| 123 | n/a | from .sharedctypes import RawValue |
|---|
| 124 | n/a | return RawValue(typecode_or_type, *args) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def RawArray(self, typecode_or_type, size_or_initializer): |
|---|
| 127 | n/a | '''Returns a shared array''' |
|---|
| 128 | n/a | from .sharedctypes import RawArray |
|---|
| 129 | n/a | return RawArray(typecode_or_type, size_or_initializer) |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def Value(self, typecode_or_type, *args, lock=True): |
|---|
| 132 | n/a | '''Returns a synchronized shared object''' |
|---|
| 133 | n/a | from .sharedctypes import Value |
|---|
| 134 | n/a | return Value(typecode_or_type, *args, lock=lock, |
|---|
| 135 | n/a | ctx=self.get_context()) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def Array(self, typecode_or_type, size_or_initializer, *, lock=True): |
|---|
| 138 | n/a | '''Returns a synchronized shared array''' |
|---|
| 139 | n/a | from .sharedctypes import Array |
|---|
| 140 | n/a | return Array(typecode_or_type, size_or_initializer, lock=lock, |
|---|
| 141 | n/a | ctx=self.get_context()) |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | def freeze_support(self): |
|---|
| 144 | n/a | '''Check whether this is a fake forked process in a frozen executable. |
|---|
| 145 | n/a | If so then run code specified by commandline and exit. |
|---|
| 146 | n/a | ''' |
|---|
| 147 | n/a | if sys.platform == 'win32' and getattr(sys, 'frozen', False): |
|---|
| 148 | n/a | from .spawn import freeze_support |
|---|
| 149 | n/a | freeze_support() |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | def get_logger(self): |
|---|
| 152 | n/a | '''Return package logger -- if it does not already exist then |
|---|
| 153 | n/a | it is created. |
|---|
| 154 | n/a | ''' |
|---|
| 155 | n/a | from .util import get_logger |
|---|
| 156 | n/a | return get_logger() |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | def log_to_stderr(self, level=None): |
|---|
| 159 | n/a | '''Turn on logging and add a handler which prints to stderr''' |
|---|
| 160 | n/a | from .util import log_to_stderr |
|---|
| 161 | n/a | return log_to_stderr(level) |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def allow_connection_pickling(self): |
|---|
| 164 | n/a | '''Install support for sending connections and sockets |
|---|
| 165 | n/a | between processes |
|---|
| 166 | n/a | ''' |
|---|
| 167 | n/a | # This is undocumented. In previous versions of multiprocessing |
|---|
| 168 | n/a | # its only effect was to make socket objects inheritable on Windows. |
|---|
| 169 | n/a | from . import connection |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | def set_executable(self, executable): |
|---|
| 172 | n/a | '''Sets the path to a python.exe or pythonw.exe binary used to run |
|---|
| 173 | n/a | child processes instead of sys.executable when using the 'spawn' |
|---|
| 174 | n/a | start method. Useful for people embedding Python. |
|---|
| 175 | n/a | ''' |
|---|
| 176 | n/a | from .spawn import set_executable |
|---|
| 177 | n/a | set_executable(executable) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def set_forkserver_preload(self, module_names): |
|---|
| 180 | n/a | '''Set list of module names to try to load in forkserver process. |
|---|
| 181 | n/a | This is really just a hint. |
|---|
| 182 | n/a | ''' |
|---|
| 183 | n/a | from .forkserver import set_forkserver_preload |
|---|
| 184 | n/a | set_forkserver_preload(module_names) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def get_context(self, method=None): |
|---|
| 187 | n/a | if method is None: |
|---|
| 188 | n/a | return self |
|---|
| 189 | n/a | try: |
|---|
| 190 | n/a | ctx = _concrete_contexts[method] |
|---|
| 191 | n/a | except KeyError: |
|---|
| 192 | n/a | raise ValueError('cannot find context for %r' % method) |
|---|
| 193 | n/a | ctx._check_available() |
|---|
| 194 | n/a | return ctx |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def get_start_method(self, allow_none=False): |
|---|
| 197 | n/a | return self._name |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | def set_start_method(self, method, force=False): |
|---|
| 200 | n/a | raise ValueError('cannot set start method of concrete context') |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | @property |
|---|
| 203 | n/a | def reducer(self): |
|---|
| 204 | n/a | '''Controls how objects will be reduced to a form that can be |
|---|
| 205 | n/a | shared with other processes.''' |
|---|
| 206 | n/a | return globals().get('reduction') |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | @reducer.setter |
|---|
| 209 | n/a | def reducer(self, reduction): |
|---|
| 210 | n/a | globals()['reduction'] = reduction |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | def _check_available(self): |
|---|
| 213 | n/a | pass |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | # |
|---|
| 216 | n/a | # Type of default context -- underlying context can be set at most once |
|---|
| 217 | n/a | # |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | class Process(process.BaseProcess): |
|---|
| 220 | n/a | _start_method = None |
|---|
| 221 | n/a | @staticmethod |
|---|
| 222 | n/a | def _Popen(process_obj): |
|---|
| 223 | n/a | return _default_context.get_context().Process._Popen(process_obj) |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | class DefaultContext(BaseContext): |
|---|
| 226 | n/a | Process = Process |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def __init__(self, context): |
|---|
| 229 | n/a | self._default_context = context |
|---|
| 230 | n/a | self._actual_context = None |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def get_context(self, method=None): |
|---|
| 233 | n/a | if method is None: |
|---|
| 234 | n/a | if self._actual_context is None: |
|---|
| 235 | n/a | self._actual_context = self._default_context |
|---|
| 236 | n/a | return self._actual_context |
|---|
| 237 | n/a | else: |
|---|
| 238 | n/a | return super().get_context(method) |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | def set_start_method(self, method, force=False): |
|---|
| 241 | n/a | if self._actual_context is not None and not force: |
|---|
| 242 | n/a | raise RuntimeError('context has already been set') |
|---|
| 243 | n/a | if method is None and force: |
|---|
| 244 | n/a | self._actual_context = None |
|---|
| 245 | n/a | return |
|---|
| 246 | n/a | self._actual_context = self.get_context(method) |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | def get_start_method(self, allow_none=False): |
|---|
| 249 | n/a | if self._actual_context is None: |
|---|
| 250 | n/a | if allow_none: |
|---|
| 251 | n/a | return None |
|---|
| 252 | n/a | self._actual_context = self._default_context |
|---|
| 253 | n/a | return self._actual_context._name |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def get_all_start_methods(self): |
|---|
| 256 | n/a | if sys.platform == 'win32': |
|---|
| 257 | n/a | return ['spawn'] |
|---|
| 258 | n/a | else: |
|---|
| 259 | n/a | if reduction.HAVE_SEND_HANDLE: |
|---|
| 260 | n/a | return ['fork', 'spawn', 'forkserver'] |
|---|
| 261 | n/a | else: |
|---|
| 262 | n/a | return ['fork', 'spawn'] |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | DefaultContext.__all__ = list(x for x in dir(DefaultContext) if x[0] != '_') |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | # |
|---|
| 267 | n/a | # Context types for fixed start method |
|---|
| 268 | n/a | # |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | if sys.platform != 'win32': |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | class ForkProcess(process.BaseProcess): |
|---|
| 273 | n/a | _start_method = 'fork' |
|---|
| 274 | n/a | @staticmethod |
|---|
| 275 | n/a | def _Popen(process_obj): |
|---|
| 276 | n/a | from .popen_fork import Popen |
|---|
| 277 | n/a | return Popen(process_obj) |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | class SpawnProcess(process.BaseProcess): |
|---|
| 280 | n/a | _start_method = 'spawn' |
|---|
| 281 | n/a | @staticmethod |
|---|
| 282 | n/a | def _Popen(process_obj): |
|---|
| 283 | n/a | from .popen_spawn_posix import Popen |
|---|
| 284 | n/a | return Popen(process_obj) |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | class ForkServerProcess(process.BaseProcess): |
|---|
| 287 | n/a | _start_method = 'forkserver' |
|---|
| 288 | n/a | @staticmethod |
|---|
| 289 | n/a | def _Popen(process_obj): |
|---|
| 290 | n/a | from .popen_forkserver import Popen |
|---|
| 291 | n/a | return Popen(process_obj) |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | class ForkContext(BaseContext): |
|---|
| 294 | n/a | _name = 'fork' |
|---|
| 295 | n/a | Process = ForkProcess |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | class SpawnContext(BaseContext): |
|---|
| 298 | n/a | _name = 'spawn' |
|---|
| 299 | n/a | Process = SpawnProcess |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | class ForkServerContext(BaseContext): |
|---|
| 302 | n/a | _name = 'forkserver' |
|---|
| 303 | n/a | Process = ForkServerProcess |
|---|
| 304 | n/a | def _check_available(self): |
|---|
| 305 | n/a | if not reduction.HAVE_SEND_HANDLE: |
|---|
| 306 | n/a | raise ValueError('forkserver start method not available') |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | _concrete_contexts = { |
|---|
| 309 | n/a | 'fork': ForkContext(), |
|---|
| 310 | n/a | 'spawn': SpawnContext(), |
|---|
| 311 | n/a | 'forkserver': ForkServerContext(), |
|---|
| 312 | n/a | } |
|---|
| 313 | n/a | _default_context = DefaultContext(_concrete_contexts['fork']) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | else: |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | class SpawnProcess(process.BaseProcess): |
|---|
| 318 | n/a | _start_method = 'spawn' |
|---|
| 319 | n/a | @staticmethod |
|---|
| 320 | n/a | def _Popen(process_obj): |
|---|
| 321 | n/a | from .popen_spawn_win32 import Popen |
|---|
| 322 | n/a | return Popen(process_obj) |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | class SpawnContext(BaseContext): |
|---|
| 325 | n/a | _name = 'spawn' |
|---|
| 326 | n/a | Process = SpawnProcess |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | _concrete_contexts = { |
|---|
| 329 | n/a | 'spawn': SpawnContext(), |
|---|
| 330 | n/a | } |
|---|
| 331 | n/a | _default_context = DefaultContext(_concrete_contexts['spawn']) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | # |
|---|
| 334 | n/a | # Force the start method |
|---|
| 335 | n/a | # |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | def _force_start_method(method): |
|---|
| 338 | n/a | _default_context._actual_context = _concrete_contexts[method] |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | # |
|---|
| 341 | n/a | # Check that the current thread is spawning a child process |
|---|
| 342 | n/a | # |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | _tls = threading.local() |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def get_spawning_popen(): |
|---|
| 347 | n/a | return getattr(_tls, 'spawning_popen', None) |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | def set_spawning_popen(popen): |
|---|
| 350 | n/a | _tls.spawning_popen = popen |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | def assert_spawning(obj): |
|---|
| 353 | n/a | if get_spawning_popen() is None: |
|---|
| 354 | n/a | raise RuntimeError( |
|---|
| 355 | n/a | '%s objects should only be shared between processes' |
|---|
| 356 | n/a | ' through inheritance' % type(obj).__name__ |
|---|
| 357 | n/a | ) |
|---|