2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Package analogous to 'threading.py' but using processes
|
|
|
|
#
|
|
|
|
# multiprocessing/__init__.py
|
|
|
|
#
|
|
|
|
# This package is intended to duplicate the functionality (and much of
|
|
|
|
# the API) of threading.py but uses processes instead of threads. A
|
|
|
|
# subpackage 'multiprocessing.dummy' has the same API but is a simple
|
|
|
|
# wrapper for 'threading'.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
2012-04-30 08:13:55 -03:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
|
|
|
__version__ = '0.70a1'
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'Process', 'current_process', 'active_children', 'freeze_support',
|
|
|
|
'Manager', 'Pipe', 'cpu_count', 'log_to_stderr', 'get_logger',
|
|
|
|
'allow_connection_pickling', 'BufferTooShort', 'TimeoutError',
|
|
|
|
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
|
2012-06-15 14:26:07 -03:00
|
|
|
'Event', 'Barrier', 'Queue', 'SimpleQueue', 'JoinableQueue', 'Pool',
|
|
|
|
'Value', 'Array', 'RawValue', 'RawArray', 'SUBDEBUG', 'SUBWARNING',
|
2013-08-14 11:35:41 -03:00
|
|
|
'set_executable', 'set_start_method', 'get_start_method',
|
|
|
|
'get_all_start_methods', 'set_forkserver_preload'
|
2008-06-11 13:44:04 -03:00
|
|
|
]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Imports
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
from .process import Process, current_process, active_children
|
|
|
|
|
|
|
|
#
|
|
|
|
# XXX These should not really be documented or public.
|
|
|
|
#
|
|
|
|
|
|
|
|
SUBDEBUG = 5
|
|
|
|
SUBWARNING = 25
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2012-10-08 09:07:00 -03:00
|
|
|
#
|
|
|
|
# Alias for main module -- will be reset by bootstrapping child processes
|
|
|
|
#
|
|
|
|
|
|
|
|
if '__main__' in sys.modules:
|
|
|
|
sys.modules['__mp_main__'] = sys.modules['__main__']
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Exceptions
|
|
|
|
#
|
|
|
|
|
|
|
|
class ProcessError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BufferTooShort(ProcessError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TimeoutError(ProcessError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AuthenticationError(ProcessError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
#
|
|
|
|
# Definitions not depending on native semaphores
|
|
|
|
#
|
|
|
|
|
|
|
|
def Manager():
|
|
|
|
'''
|
|
|
|
Returns a manager associated with a running server process
|
|
|
|
|
|
|
|
The managers methods such as `Lock()`, `Condition()` and `Queue()`
|
|
|
|
can be used to create shared objects.
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .managers import SyncManager
|
2008-06-11 13:44:04 -03:00
|
|
|
m = SyncManager()
|
|
|
|
m.start()
|
|
|
|
return m
|
|
|
|
|
|
|
|
def Pipe(duplex=True):
|
|
|
|
'''
|
|
|
|
Returns two connection object connected by a pipe
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .connection import Pipe
|
2008-06-11 13:44:04 -03:00
|
|
|
return Pipe(duplex)
|
|
|
|
|
|
|
|
def cpu_count():
|
|
|
|
'''
|
|
|
|
Returns the number of CPUs in the system
|
|
|
|
'''
|
2013-05-20 09:40:46 -03:00
|
|
|
num = os.cpu_count()
|
|
|
|
if num is None:
|
|
|
|
raise NotImplementedError('cannot determine number of cpus')
|
2008-06-11 13:44:04 -03:00
|
|
|
else:
|
|
|
|
return num
|
|
|
|
|
|
|
|
def freeze_support():
|
|
|
|
'''
|
|
|
|
Check whether this is a fake forked process in a frozen executable.
|
|
|
|
If so then run code specified by commandline and exit.
|
|
|
|
'''
|
|
|
|
if sys.platform == 'win32' and getattr(sys, 'frozen', False):
|
2013-08-14 11:35:41 -03:00
|
|
|
from .spawn import freeze_support
|
2008-06-11 13:44:04 -03:00
|
|
|
freeze_support()
|
|
|
|
|
|
|
|
def get_logger():
|
|
|
|
'''
|
|
|
|
Return package logger -- if it does not already exist then it is created
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .util import get_logger
|
2008-06-11 13:44:04 -03:00
|
|
|
return get_logger()
|
|
|
|
|
|
|
|
def log_to_stderr(level=None):
|
|
|
|
'''
|
|
|
|
Turn on logging and add a handler which prints to stderr
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .util import log_to_stderr
|
2008-06-11 13:44:04 -03:00
|
|
|
return log_to_stderr(level)
|
|
|
|
|
|
|
|
def allow_connection_pickling():
|
|
|
|
'''
|
|
|
|
Install support for sending connections and sockets between processes
|
|
|
|
'''
|
2012-04-24 17:56:57 -03:00
|
|
|
# This is undocumented. In previous versions of multiprocessing
|
|
|
|
# its only effect was to make socket objects inheritable on Windows.
|
2013-08-14 11:35:41 -03:00
|
|
|
from . import connection
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Definitions depending on native semaphores
|
|
|
|
#
|
|
|
|
|
|
|
|
def Lock():
|
|
|
|
'''
|
|
|
|
Returns a non-recursive lock object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import Lock
|
2008-06-11 13:44:04 -03:00
|
|
|
return Lock()
|
|
|
|
|
|
|
|
def RLock():
|
|
|
|
'''
|
|
|
|
Returns a recursive lock object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import RLock
|
2008-06-11 13:44:04 -03:00
|
|
|
return RLock()
|
|
|
|
|
|
|
|
def Condition(lock=None):
|
|
|
|
'''
|
|
|
|
Returns a condition object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import Condition
|
2008-06-11 13:44:04 -03:00
|
|
|
return Condition(lock)
|
|
|
|
|
|
|
|
def Semaphore(value=1):
|
|
|
|
'''
|
|
|
|
Returns a semaphore object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import Semaphore
|
2008-06-11 13:44:04 -03:00
|
|
|
return Semaphore(value)
|
|
|
|
|
|
|
|
def BoundedSemaphore(value=1):
|
|
|
|
'''
|
|
|
|
Returns a bounded semaphore object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import BoundedSemaphore
|
2008-06-11 13:44:04 -03:00
|
|
|
return BoundedSemaphore(value)
|
|
|
|
|
|
|
|
def Event():
|
|
|
|
'''
|
|
|
|
Returns an event object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import Event
|
2008-06-11 13:44:04 -03:00
|
|
|
return Event()
|
|
|
|
|
2012-06-15 14:26:07 -03:00
|
|
|
def Barrier(parties, action=None, timeout=None):
|
|
|
|
'''
|
|
|
|
Returns a barrier object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .synchronize import Barrier
|
2012-06-15 14:26:07 -03:00
|
|
|
return Barrier(parties, action, timeout)
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
def Queue(maxsize=0):
|
|
|
|
'''
|
|
|
|
Returns a queue object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .queues import Queue
|
2008-06-11 13:44:04 -03:00
|
|
|
return Queue(maxsize)
|
|
|
|
|
|
|
|
def JoinableQueue(maxsize=0):
|
|
|
|
'''
|
|
|
|
Returns a queue object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .queues import JoinableQueue
|
2008-06-11 13:44:04 -03:00
|
|
|
return JoinableQueue(maxsize)
|
|
|
|
|
2012-02-15 18:27:00 -04:00
|
|
|
def SimpleQueue():
|
|
|
|
'''
|
|
|
|
Returns a queue object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .queues import SimpleQueue
|
2012-02-15 18:27:00 -04:00
|
|
|
return SimpleQueue()
|
|
|
|
|
2010-01-26 23:36:01 -04:00
|
|
|
def Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Returns a process pool object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .pool import Pool
|
2010-01-26 23:36:01 -04:00
|
|
|
return Pool(processes, initializer, initargs, maxtasksperchild)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def RawValue(typecode_or_type, *args):
|
|
|
|
'''
|
|
|
|
Returns a shared object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .sharedctypes import RawValue
|
2008-06-11 13:44:04 -03:00
|
|
|
return RawValue(typecode_or_type, *args)
|
|
|
|
|
|
|
|
def RawArray(typecode_or_type, size_or_initializer):
|
|
|
|
'''
|
|
|
|
Returns a shared array
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .sharedctypes import RawArray
|
2008-06-11 13:44:04 -03:00
|
|
|
return RawArray(typecode_or_type, size_or_initializer)
|
|
|
|
|
2012-05-29 08:01:47 -03:00
|
|
|
def Value(typecode_or_type, *args, lock=True):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Returns a synchronized shared object
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .sharedctypes import Value
|
2012-05-29 08:01:47 -03:00
|
|
|
return Value(typecode_or_type, *args, lock=lock)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2012-05-29 08:01:47 -03:00
|
|
|
def Array(typecode_or_type, size_or_initializer, *, lock=True):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Returns a synchronized shared array
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
from .sharedctypes import Array
|
2012-05-29 08:01:47 -03:00
|
|
|
return Array(typecode_or_type, size_or_initializer, lock=lock)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
def set_executable(executable):
|
|
|
|
'''
|
|
|
|
Sets the path to a python.exe or pythonw.exe binary used to run
|
|
|
|
child processes instead of sys.executable when using the 'spawn'
|
|
|
|
start method. Useful for people embedding Python.
|
|
|
|
'''
|
|
|
|
from .spawn import set_executable
|
|
|
|
set_executable(executable)
|
|
|
|
|
|
|
|
def set_start_method(method):
|
|
|
|
'''
|
|
|
|
Set method for starting processes: 'fork', 'spawn' or 'forkserver'.
|
|
|
|
'''
|
|
|
|
from .popen import set_start_method
|
|
|
|
set_start_method(method)
|
|
|
|
|
|
|
|
def get_start_method():
|
|
|
|
'''
|
|
|
|
Get method for starting processes: 'fork', 'spawn' or 'forkserver'.
|
|
|
|
'''
|
|
|
|
from .popen import get_start_method
|
|
|
|
return get_start_method()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
def get_all_start_methods():
|
|
|
|
'''
|
|
|
|
Get list of availables start methods, default first.
|
|
|
|
'''
|
|
|
|
from .popen import get_all_start_methods
|
|
|
|
return get_all_start_methods()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
def set_forkserver_preload(module_names):
|
|
|
|
'''
|
|
|
|
Set list of module names to try to load in the forkserver process
|
|
|
|
when it is started. Properly chosen this can significantly reduce
|
|
|
|
the cost of starting a new process using the forkserver method.
|
|
|
|
The default list is ['__main__'].
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
from .forkserver import set_forkserver_preload
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
set_forkserver_preload(module_names)
|