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'
54 __author__
= 'R. Oudkerk (r.m.oudkerk@gmail.com)'
63 from multiprocessing
.process
import Process
, current_process
, active_children
69 class ProcessError(Exception):
72 class BufferTooShort(ProcessError
):
75 class TimeoutError(ProcessError
):
78 class AuthenticationError(ProcessError
):
81 # This is down here because _multiprocessing uses BufferTooShort
82 import _multiprocessing
85 # Definitions not depending on native semaphores
90 Returns a manager associated with a running server process
92 The managers methods such as `Lock()`, `Condition()` and `Queue()`
93 can be used to create shared objects.
95 from multiprocessing
.managers
import SyncManager
100 def Pipe(duplex
=True):
102 Returns two connection object connected by a pipe
104 from multiprocessing
.connection
import Pipe
109 Returns the number of CPUs in the system
111 if sys
.platform
== 'win32':
113 num
= int(os
.environ
['NUMBER_OF_PROCESSORS'])
114 except (ValueError, KeyError):
116 elif 'bsd' in sys
.platform
or sys
.platform
== 'darwin':
118 num
= int(os
.popen('sysctl -n hw.ncpu').read())
123 num
= os
.sysconf('SC_NPROCESSORS_ONLN')
124 except (ValueError, OSError, AttributeError):
130 raise NotImplementedError('cannot determine number of cpus')
132 def freeze_support():
134 Check whether this is a fake forked process in a frozen executable.
135 If so then run code specified by commandline and exit.
137 if sys
.platform
== 'win32' and getattr(sys
, 'frozen', False):
138 from multiprocessing
.forking
import freeze_support
143 Return package logger -- if it does not already exist then it is created
145 from multiprocessing
.util
import get_logger
148 def log_to_stderr(level
=None):
150 Turn on logging and add a handler which prints to stderr
152 from multiprocessing
.util
import log_to_stderr
153 return log_to_stderr(level
)
155 def allow_connection_pickling():
157 Install support for sending connections and sockets between processes
159 from multiprocessing
import reduction
162 # Definitions depending on native semaphores
167 Returns a non-recursive lock object
169 from multiprocessing
.synchronize
import Lock
174 Returns a recursive lock object
176 from multiprocessing
.synchronize
import RLock
179 def Condition(lock
=None):
181 Returns a condition object
183 from multiprocessing
.synchronize
import Condition
184 return Condition(lock
)
186 def Semaphore(value
=1):
188 Returns a semaphore object
190 from multiprocessing
.synchronize
import Semaphore
191 return Semaphore(value
)
193 def BoundedSemaphore(value
=1):
195 Returns a bounded semaphore object
197 from multiprocessing
.synchronize
import BoundedSemaphore
198 return BoundedSemaphore(value
)
202 Returns an event object
204 from multiprocessing
.synchronize
import Event
207 def Queue(maxsize
=0):
209 Returns a queue object
211 from multiprocessing
.queues
import Queue
212 return Queue(maxsize
)
214 def JoinableQueue(maxsize
=0):
216 Returns a queue object
218 from multiprocessing
.queues
import JoinableQueue
219 return JoinableQueue(maxsize
)
221 def Pool(processes
=None, initializer
=None, initargs
=()):
223 Returns a process pool object
225 from multiprocessing
.pool
import Pool
226 return Pool(processes
, initializer
, initargs
)
228 def RawValue(typecode_or_type
, *args
):
230 Returns a shared object
232 from multiprocessing
.sharedctypes
import RawValue
233 return RawValue(typecode_or_type
, *args
)
235 def RawArray(typecode_or_type
, size_or_initializer
):
237 Returns a shared array
239 from multiprocessing
.sharedctypes
import RawArray
240 return RawArray(typecode_or_type
, size_or_initializer
)
242 def Value(typecode_or_type
, *args
, **kwds
):
244 Returns a synchronized shared object
246 from multiprocessing
.sharedctypes
import Value
247 return Value(typecode_or_type
, *args
, **kwds
)
249 def Array(typecode_or_type
, size_or_initializer
, **kwds
):
251 Returns a synchronized shared array
253 from multiprocessing
.sharedctypes
import Array
254 return Array(typecode_or_type
, size_or_initializer
, **kwds
)
260 if sys
.platform
== 'win32':
262 def set_executable(executable
):
264 Sets the path to a python.exe or pythonw.exe binary used to run
265 child processes on Windows instead of sys.executable.
266 Useful for people embedding Python.
268 from multiprocessing
.forking
import set_executable
269 set_executable(executable
)
271 __all__
+= ['set_executable']