2 # Package analogous to 'threading.py' but using processes
4 # multiprocessing/__init__.py
6 # This package is intended to duplicate the functionality (and much of
7 # the API) of threading.py but uses processes instead of threads. A
8 # subpackage 'multiprocessing.dummy' has the same API but is a simple
9 # wrapper for 'threading'.
11 # Try calling `multiprocessing.doc.main()` to read the html
12 # documentation in in a webbrowser.
15 # Copyright (c) 2006-2008, R Oudkerk
16 # All rights reserved.
18 # Redistribution and use in source and binary forms, with or without
19 # modification, are permitted provided that the following conditions
22 # 1. Redistributions of source code must retain the above copyright
23 # notice, this list of conditions and the following disclaimer.
24 # 2. Redistributions in binary form must reproduce the above copyright
25 # notice, this list of conditions and the following disclaimer in the
26 # documentation and/or other materials provided with the distribution.
27 # 3. Neither the name of author nor the names of any contributors may be
28 # used to endorse or promote products derived from this software
29 # without specific prior written permission.
31 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
32 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 __version__
= '0.70a1'
46 'Process', 'current_process', 'active_children', 'freeze_support',
47 'Manager', 'Pipe', 'cpu_count', 'log_to_stderr', 'get_logger',
48 'allow_connection_pickling', 'BufferTooShort', 'TimeoutError',
49 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition',
50 'Event', 'Queue', 'JoinableQueue', 'Pool', 'Value', 'Array',
51 'RawValue', 'RawArray', 'SUBDEBUG', 'SUBWARNING',
54 __author__
= 'R. Oudkerk (r.m.oudkerk@gmail.com)'
63 from multiprocessing
.process
import Process
, current_process
, active_children
64 from multiprocessing
.util
import SUBDEBUG
, SUBWARNING
70 class ProcessError(Exception):
73 class BufferTooShort(ProcessError
):
76 class TimeoutError(ProcessError
):
79 class AuthenticationError(ProcessError
):
82 # This is down here because _multiprocessing uses BufferTooShort
83 import _multiprocessing
86 # Definitions not depending on native semaphores
91 Returns a manager associated with a running server process
93 The managers methods such as `Lock()`, `Condition()` and `Queue()`
94 can be used to create shared objects.
96 from multiprocessing
.managers
import SyncManager
101 def Pipe(duplex
=True):
103 Returns two connection object connected by a pipe
105 from multiprocessing
.connection
import Pipe
110 Returns the number of CPUs in the system
112 if sys
.platform
== 'win32':
114 num
= int(os
.environ
['NUMBER_OF_PROCESSORS'])
115 except (ValueError, KeyError):
117 elif 'bsd' in sys
.platform
or sys
.platform
== 'darwin':
119 num
= int(os
.popen('sysctl -n hw.ncpu').read())
124 num
= os
.sysconf('SC_NPROCESSORS_ONLN')
125 except (ValueError, OSError, AttributeError):
131 raise NotImplementedError('cannot determine number of cpus')
133 def freeze_support():
135 Check whether this is a fake forked process in a frozen executable.
136 If so then run code specified by commandline and exit.
138 if sys
.platform
== 'win32' and getattr(sys
, 'frozen', False):
139 from multiprocessing
.forking
import freeze_support
144 Return package logger -- if it does not already exist then it is created
146 from multiprocessing
.util
import get_logger
149 def log_to_stderr(level
=None):
151 Turn on logging and add a handler which prints to stderr
153 from multiprocessing
.util
import log_to_stderr
154 return log_to_stderr(level
)
156 def allow_connection_pickling():
158 Install support for sending connections and sockets between processes
160 from multiprocessing
import reduction
163 # Definitions depending on native semaphores
168 Returns a non-recursive lock object
170 from multiprocessing
.synchronize
import Lock
175 Returns a recursive lock object
177 from multiprocessing
.synchronize
import RLock
180 def Condition(lock
=None):
182 Returns a condition object
184 from multiprocessing
.synchronize
import Condition
185 return Condition(lock
)
187 def Semaphore(value
=1):
189 Returns a semaphore object
191 from multiprocessing
.synchronize
import Semaphore
192 return Semaphore(value
)
194 def BoundedSemaphore(value
=1):
196 Returns a bounded semaphore object
198 from multiprocessing
.synchronize
import BoundedSemaphore
199 return BoundedSemaphore(value
)
203 Returns an event object
205 from multiprocessing
.synchronize
import Event
208 def Queue(maxsize
=0):
210 Returns a queue object
212 from multiprocessing
.queues
import Queue
213 return Queue(maxsize
)
215 def JoinableQueue(maxsize
=0):
217 Returns a queue object
219 from multiprocessing
.queues
import JoinableQueue
220 return JoinableQueue(maxsize
)
222 def Pool(processes
=None, initializer
=None, initargs
=()):
224 Returns a process pool object
226 from multiprocessing
.pool
import Pool
227 return Pool(processes
, initializer
, initargs
)
229 def RawValue(typecode_or_type
, *args
):
231 Returns a shared object
233 from multiprocessing
.sharedctypes
import RawValue
234 return RawValue(typecode_or_type
, *args
)
236 def RawArray(typecode_or_type
, size_or_initializer
):
238 Returns a shared array
240 from multiprocessing
.sharedctypes
import RawArray
241 return RawArray(typecode_or_type
, size_or_initializer
)
243 def Value(typecode_or_type
, *args
, **kwds
):
245 Returns a synchronized shared object
247 from multiprocessing
.sharedctypes
import Value
248 return Value(typecode_or_type
, *args
, **kwds
)
250 def Array(typecode_or_type
, size_or_initializer
, **kwds
):
252 Returns a synchronized shared array
254 from multiprocessing
.sharedctypes
import Array
255 return Array(typecode_or_type
, size_or_initializer
, **kwds
)
261 if sys
.platform
== 'win32':
263 def set_executable(executable
):
265 Sets the path to a python.exe or pythonw.exe binary used to run
266 child processes on Windows instead of sys.executable.
267 Useful for people embedding Python.
269 from multiprocessing
.forking
import set_executable
270 set_executable(executable
)
272 __all__
+= ['set_executable']