Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Package analogous to 'threading.py' but using processes |
| 3 | # |
| 4 | # multiprocessing/__init__.py |
| 5 | # |
| 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'. |
| 10 | # |
| 11 | # Try calling `multiprocessing.doc.main()` to read the html |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 12 | # documentation in a webbrowser. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 13 | # |
| 14 | # |
| 15 | # Copyright (c) 2006-2008, R Oudkerk |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 16 | # Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 17 | # |
| 18 | |
| 19 | __version__ = '0.70a1' |
| 20 | |
| 21 | __all__ = [ |
| 22 | 'Process', 'current_process', 'active_children', 'freeze_support', |
| 23 | 'Manager', 'Pipe', 'cpu_count', 'log_to_stderr', 'get_logger', |
| 24 | 'allow_connection_pickling', 'BufferTooShort', 'TimeoutError', |
| 25 | 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', |
Richard Oudkerk | 3730a17 | 2012-06-15 18:26:07 +0100 | [diff] [blame] | 26 | 'Event', 'Barrier', 'Queue', 'SimpleQueue', 'JoinableQueue', 'Pool', |
| 27 | 'Value', 'Array', 'RawValue', 'RawArray', 'SUBDEBUG', 'SUBWARNING', |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 28 | ] |
| 29 | |
| 30 | __author__ = 'R. Oudkerk (r.m.oudkerk@gmail.com)' |
| 31 | |
| 32 | # |
| 33 | # Imports |
| 34 | # |
| 35 | |
| 36 | import os |
| 37 | import sys |
| 38 | |
| 39 | from multiprocessing.process import Process, current_process, active_children |
Jesse Noller | 41faa54 | 2009-01-25 03:45:53 +0000 | [diff] [blame] | 40 | from multiprocessing.util import SUBDEBUG, SUBWARNING |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 41 | |
| 42 | # |
| 43 | # Exceptions |
| 44 | # |
| 45 | |
| 46 | class ProcessError(Exception): |
| 47 | pass |
| 48 | |
| 49 | class BufferTooShort(ProcessError): |
| 50 | pass |
| 51 | |
| 52 | class TimeoutError(ProcessError): |
| 53 | pass |
| 54 | |
| 55 | class AuthenticationError(ProcessError): |
| 56 | pass |
| 57 | |
| 58 | import _multiprocessing |
| 59 | |
| 60 | # |
| 61 | # Definitions not depending on native semaphores |
| 62 | # |
| 63 | |
| 64 | def Manager(): |
| 65 | ''' |
| 66 | Returns a manager associated with a running server process |
| 67 | |
| 68 | The managers methods such as `Lock()`, `Condition()` and `Queue()` |
| 69 | can be used to create shared objects. |
| 70 | ''' |
| 71 | from multiprocessing.managers import SyncManager |
| 72 | m = SyncManager() |
| 73 | m.start() |
| 74 | return m |
| 75 | |
| 76 | def Pipe(duplex=True): |
| 77 | ''' |
| 78 | Returns two connection object connected by a pipe |
| 79 | ''' |
| 80 | from multiprocessing.connection import Pipe |
| 81 | return Pipe(duplex) |
| 82 | |
| 83 | def cpu_count(): |
| 84 | ''' |
| 85 | Returns the number of CPUs in the system |
| 86 | ''' |
| 87 | if sys.platform == 'win32': |
| 88 | try: |
| 89 | num = int(os.environ['NUMBER_OF_PROCESSORS']) |
| 90 | except (ValueError, KeyError): |
| 91 | num = 0 |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 92 | elif 'bsd' in sys.platform or sys.platform == 'darwin': |
Ronald Oussoren | 0b8753d | 2011-03-16 09:41:32 -0400 | [diff] [blame] | 93 | comm = '/sbin/sysctl -n hw.ncpu' |
| 94 | if sys.platform == 'darwin': |
| 95 | comm = '/usr' + comm |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 96 | try: |
Ronald Oussoren | 0b8753d | 2011-03-16 09:41:32 -0400 | [diff] [blame] | 97 | with os.popen(comm) as p: |
Brian Curtin | 50be1ca | 2010-11-01 05:10:44 +0000 | [diff] [blame] | 98 | num = int(p.read()) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 99 | except ValueError: |
| 100 | num = 0 |
| 101 | else: |
| 102 | try: |
| 103 | num = os.sysconf('SC_NPROCESSORS_ONLN') |
| 104 | except (ValueError, OSError, AttributeError): |
| 105 | num = 0 |
| 106 | |
| 107 | if num >= 1: |
| 108 | return num |
| 109 | else: |
| 110 | raise NotImplementedError('cannot determine number of cpus') |
| 111 | |
| 112 | def freeze_support(): |
| 113 | ''' |
| 114 | Check whether this is a fake forked process in a frozen executable. |
| 115 | If so then run code specified by commandline and exit. |
| 116 | ''' |
| 117 | if sys.platform == 'win32' and getattr(sys, 'frozen', False): |
| 118 | from multiprocessing.forking import freeze_support |
| 119 | freeze_support() |
| 120 | |
| 121 | def get_logger(): |
| 122 | ''' |
| 123 | Return package logger -- if it does not already exist then it is created |
| 124 | ''' |
| 125 | from multiprocessing.util import get_logger |
| 126 | return get_logger() |
| 127 | |
| 128 | def log_to_stderr(level=None): |
| 129 | ''' |
| 130 | Turn on logging and add a handler which prints to stderr |
| 131 | ''' |
| 132 | from multiprocessing.util import log_to_stderr |
| 133 | return log_to_stderr(level) |
| 134 | |
| 135 | def allow_connection_pickling(): |
| 136 | ''' |
| 137 | Install support for sending connections and sockets between processes |
| 138 | ''' |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 139 | # This is undocumented. In previous versions of multiprocessing |
| 140 | # its only effect was to make socket objects inheritable on Windows. |
| 141 | import multiprocessing.connection |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 142 | |
| 143 | # |
| 144 | # Definitions depending on native semaphores |
| 145 | # |
| 146 | |
| 147 | def Lock(): |
| 148 | ''' |
| 149 | Returns a non-recursive lock object |
| 150 | ''' |
| 151 | from multiprocessing.synchronize import Lock |
| 152 | return Lock() |
| 153 | |
| 154 | def RLock(): |
| 155 | ''' |
| 156 | Returns a recursive lock object |
| 157 | ''' |
| 158 | from multiprocessing.synchronize import RLock |
| 159 | return RLock() |
| 160 | |
| 161 | def Condition(lock=None): |
| 162 | ''' |
| 163 | Returns a condition object |
| 164 | ''' |
| 165 | from multiprocessing.synchronize import Condition |
| 166 | return Condition(lock) |
| 167 | |
| 168 | def Semaphore(value=1): |
| 169 | ''' |
| 170 | Returns a semaphore object |
| 171 | ''' |
| 172 | from multiprocessing.synchronize import Semaphore |
| 173 | return Semaphore(value) |
| 174 | |
| 175 | def BoundedSemaphore(value=1): |
| 176 | ''' |
| 177 | Returns a bounded semaphore object |
| 178 | ''' |
| 179 | from multiprocessing.synchronize import BoundedSemaphore |
| 180 | return BoundedSemaphore(value) |
| 181 | |
| 182 | def Event(): |
| 183 | ''' |
| 184 | Returns an event object |
| 185 | ''' |
| 186 | from multiprocessing.synchronize import Event |
| 187 | return Event() |
| 188 | |
Richard Oudkerk | 3730a17 | 2012-06-15 18:26:07 +0100 | [diff] [blame] | 189 | def Barrier(parties, action=None, timeout=None): |
| 190 | ''' |
| 191 | Returns a barrier object |
| 192 | ''' |
| 193 | from multiprocessing.synchronize import Barrier |
| 194 | return Barrier(parties, action, timeout) |
| 195 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 196 | def Queue(maxsize=0): |
| 197 | ''' |
| 198 | Returns a queue object |
| 199 | ''' |
| 200 | from multiprocessing.queues import Queue |
| 201 | return Queue(maxsize) |
| 202 | |
| 203 | def JoinableQueue(maxsize=0): |
| 204 | ''' |
| 205 | Returns a queue object |
| 206 | ''' |
| 207 | from multiprocessing.queues import JoinableQueue |
| 208 | return JoinableQueue(maxsize) |
| 209 | |
Sandro Tosi | cd77815 | 2012-02-15 23:27:00 +0100 | [diff] [blame] | 210 | def SimpleQueue(): |
| 211 | ''' |
| 212 | Returns a queue object |
| 213 | ''' |
| 214 | from multiprocessing.queues import SimpleQueue |
| 215 | return SimpleQueue() |
| 216 | |
Jesse Noller | 1f0b658 | 2010-01-27 03:36:01 +0000 | [diff] [blame] | 217 | def Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 218 | ''' |
| 219 | Returns a process pool object |
| 220 | ''' |
| 221 | from multiprocessing.pool import Pool |
Jesse Noller | 1f0b658 | 2010-01-27 03:36:01 +0000 | [diff] [blame] | 222 | return Pool(processes, initializer, initargs, maxtasksperchild) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 223 | |
| 224 | def RawValue(typecode_or_type, *args): |
| 225 | ''' |
| 226 | Returns a shared object |
| 227 | ''' |
| 228 | from multiprocessing.sharedctypes import RawValue |
| 229 | return RawValue(typecode_or_type, *args) |
| 230 | |
| 231 | def RawArray(typecode_or_type, size_or_initializer): |
| 232 | ''' |
| 233 | Returns a shared array |
| 234 | ''' |
| 235 | from multiprocessing.sharedctypes import RawArray |
| 236 | return RawArray(typecode_or_type, size_or_initializer) |
| 237 | |
Richard Oudkerk | 87ea780 | 2012-05-29 12:01:47 +0100 | [diff] [blame] | 238 | def Value(typecode_or_type, *args, lock=True): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 239 | ''' |
| 240 | Returns a synchronized shared object |
| 241 | ''' |
| 242 | from multiprocessing.sharedctypes import Value |
Richard Oudkerk | 87ea780 | 2012-05-29 12:01:47 +0100 | [diff] [blame] | 243 | return Value(typecode_or_type, *args, lock=lock) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 244 | |
Richard Oudkerk | 87ea780 | 2012-05-29 12:01:47 +0100 | [diff] [blame] | 245 | def Array(typecode_or_type, size_or_initializer, *, lock=True): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 246 | ''' |
| 247 | Returns a synchronized shared array |
| 248 | ''' |
| 249 | from multiprocessing.sharedctypes import Array |
Richard Oudkerk | 87ea780 | 2012-05-29 12:01:47 +0100 | [diff] [blame] | 250 | return Array(typecode_or_type, size_or_initializer, lock=lock) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 251 | |
| 252 | # |
| 253 | # |
| 254 | # |
| 255 | |
| 256 | if sys.platform == 'win32': |
| 257 | |
| 258 | def set_executable(executable): |
| 259 | ''' |
| 260 | Sets the path to a python.exe or pythonw.exe binary used to run |
| 261 | child processes on Windows instead of sys.executable. |
| 262 | Useful for people embedding Python. |
| 263 | ''' |
| 264 | from multiprocessing.forking import set_executable |
| 265 | set_executable(executable) |
| 266 | |
| 267 | __all__ += ['set_executable'] |