blob: dbaa64e2482c2d2cc168dd852ee4a3c4dbd18fd9 [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001:mod:`multiprocessing` --- Process-based "threading" interface
2==============================================================
3
4.. module:: multiprocessing
5 :synopsis: Process-based "threading" interface.
6
Benjamin Petersone711caf2008-06-11 16:44:04 +00007
8Introduction
Georg Brandl49702152008-09-29 06:43:45 +00009------------
Benjamin Petersone711caf2008-06-11 16:44:04 +000010
Benjamin Peterson5289b2b2008-06-28 00:40:54 +000011:mod:`multiprocessing` is a package that supports spawning processes using an
12API similar to the :mod:`threading` module. The :mod:`multiprocessing` package
13offers both local and remote concurrency, effectively side-stepping the
14:term:`Global Interpreter Lock` by using subprocesses instead of threads. Due
15to this, the :mod:`multiprocessing` module allows the programmer to fully
16leverage multiple processors on a given machine. It runs on both Unix and
17Windows.
Benjamin Petersone711caf2008-06-11 16:44:04 +000018
Benjamin Petersone5384b02008-10-04 22:00:42 +000019.. warning::
20
21 Some of this package's functionality requires a functioning shared semaphore
22 implementation on the host operating system. Without one, the
23 :mod:`multiprocessing.synchronize` module will be disabled, and attempts to
24 import it will result in an :exc:`ImportError`. See
25 :issue:`3770` for additional information.
Benjamin Petersone711caf2008-06-11 16:44:04 +000026
Jesse Noller45239682008-11-28 18:46:19 +000027.. note::
28
29 Functionality within this package requires that the ``__main__`` method be
30 importable by the children. This is covered in :ref:`multiprocessing-programming`
31 however it is worth pointing out here. This means that some examples, such
32 as the :class:`multiprocessing.Pool` examples will not work in the
33 interactive interpreter. For example::
34
35 >>> from multiprocessing import Pool
36 >>> p = Pool(5)
37 >>> def f(x):
38 ... return x*x
39 ...
40 >>> p.map(f, [1,2,3])
41 Process PoolWorker-1:
42 Process PoolWorker-2:
43 Traceback (most recent call last):
44 Traceback (most recent call last):
45 AttributeError: 'module' object has no attribute 'f'
46 AttributeError: 'module' object has no attribute 'f'
47 AttributeError: 'module' object has no attribute 'f'
48
49
Benjamin Petersone711caf2008-06-11 16:44:04 +000050The :class:`Process` class
51~~~~~~~~~~~~~~~~~~~~~~~~~~
52
53In :mod:`multiprocessing`, processes are spawned by creating a :class:`Process`
Benjamin Peterson5289b2b2008-06-28 00:40:54 +000054object and then calling its :meth:`~Process.start` method. :class:`Process`
Benjamin Petersone711caf2008-06-11 16:44:04 +000055follows the API of :class:`threading.Thread`. A trivial example of a
56multiprocess program is ::
57
Jesse Noller45239682008-11-28 18:46:19 +000058 from multiprocessing import Process
Benjamin Petersone711caf2008-06-11 16:44:04 +000059
60 def f(name):
Georg Brandl49702152008-09-29 06:43:45 +000061 print('hello', name)
Benjamin Petersone711caf2008-06-11 16:44:04 +000062
Jesse Noller45239682008-11-28 18:46:19 +000063 if __name__ == '__main__':
64 p = Process(target=f, args=('bob',))
65 p.start()
66 p.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +000067
Jesse Noller45239682008-11-28 18:46:19 +000068To show the individual process IDs involved, here is an expanded example::
69
70 from multiprocessing import Process
71 import os
72
73 def info(title):
74 print title
75 print 'module name:', __name__
76 print 'parent process:', os.getppid()
77 print 'process id:', os.getpid()
78
79 def f(name):
80 info('function f')
81 print 'hello', name
82
83 if __name__ == '__main__':
84 info('main line')
85 p = Process(target=f, args=('bob',))
86 p.start()
87 p.join()
Benjamin Petersone711caf2008-06-11 16:44:04 +000088
89For an explanation of why (on Windows) the ``if __name__ == '__main__'`` part is
90necessary, see :ref:`multiprocessing-programming`.
91
92
93
94Exchanging objects between processes
95~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96
97:mod:`multiprocessing` supports two types of communication channel between
98processes:
99
100**Queues**
101
Benjamin Peterson257060a2008-06-28 01:42:41 +0000102 The :class:`Queue` class is a near clone of :class:`queue.Queue`. For
Benjamin Petersone711caf2008-06-11 16:44:04 +0000103 example::
104
105 from multiprocessing import Process, Queue
106
107 def f(q):
108 q.put([42, None, 'hello'])
109
110 if __name__ == '__main__':
111 q = Queue()
112 p = Process(target=f, args=(q,))
113 p.start()
Georg Brandl49702152008-09-29 06:43:45 +0000114 print(q.get()) # prints "[42, None, 'hello']"
Benjamin Petersone711caf2008-06-11 16:44:04 +0000115 p.join()
116
117 Queues are thread and process safe.
118
119**Pipes**
120
121 The :func:`Pipe` function returns a pair of connection objects connected by a
122 pipe which by default is duplex (two-way). For example::
123
124 from multiprocessing import Process, Pipe
125
126 def f(conn):
127 conn.send([42, None, 'hello'])
128 conn.close()
129
130 if __name__ == '__main__':
131 parent_conn, child_conn = Pipe()
132 p = Process(target=f, args=(child_conn,))
133 p.start()
Georg Brandl49702152008-09-29 06:43:45 +0000134 print(parent_conn.recv()) # prints "[42, None, 'hello']"
Benjamin Petersone711caf2008-06-11 16:44:04 +0000135 p.join()
136
137 The two connection objects returned by :func:`Pipe` represent the two ends of
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000138 the pipe. Each connection object has :meth:`~Connection.send` and
139 :meth:`~Connection.recv` methods (among others). Note that data in a pipe
140 may become corrupted if two processes (or threads) try to read from or write
141 to the *same* end of the pipe at the same time. Of course there is no risk
142 of corruption from processes using different ends of the pipe at the same
143 time.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000144
145
146Synchronization between processes
147~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148
149:mod:`multiprocessing` contains equivalents of all the synchronization
150primitives from :mod:`threading`. For instance one can use a lock to ensure
151that only one process prints to standard output at a time::
152
153 from multiprocessing import Process, Lock
154
155 def f(l, i):
156 l.acquire()
Georg Brandl49702152008-09-29 06:43:45 +0000157 print('hello world', i)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000158 l.release()
159
160 if __name__ == '__main__':
161 lock = Lock()
162
163 for num in range(10):
164 Process(target=f, args=(lock, num)).start()
165
166Without using the lock output from the different processes is liable to get all
167mixed up.
168
169
170Sharing state between processes
171~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172
173As mentioned above, when doing concurrent programming it is usually best to
174avoid using shared state as far as possible. This is particularly true when
175using multiple processes.
176
177However, if you really do need to use some shared data then
178:mod:`multiprocessing` provides a couple of ways of doing so.
179
180**Shared memory**
181
182 Data can be stored in a shared memory map using :class:`Value` or
183 :class:`Array`. For example, the following code ::
184
185 from multiprocessing import Process, Value, Array
186
187 def f(n, a):
188 n.value = 3.1415927
189 for i in range(len(a)):
190 a[i] = -a[i]
191
192 if __name__ == '__main__':
193 num = Value('d', 0.0)
194 arr = Array('i', range(10))
195
196 p = Process(target=f, args=(num, arr))
197 p.start()
198 p.join()
199
Georg Brandl49702152008-09-29 06:43:45 +0000200 print(num.value)
201 print(arr[:])
Benjamin Petersone711caf2008-06-11 16:44:04 +0000202
203 will print ::
204
205 3.1415927
206 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
207
208 The ``'d'`` and ``'i'`` arguments used when creating ``num`` and ``arr`` are
209 typecodes of the kind used by the :mod:`array` module: ``'d'`` indicates a
Georg Brandl2ee470f2008-07-16 12:55:28 +0000210 double precision float and ``'i'`` indicates a signed integer. These shared
Benjamin Petersone711caf2008-06-11 16:44:04 +0000211 objects will be process and thread safe.
212
213 For more flexibility in using shared memory one can use the
214 :mod:`multiprocessing.sharedctypes` module which supports the creation of
215 arbitrary ctypes objects allocated from shared memory.
216
217**Server process**
218
219 A manager object returned by :func:`Manager` controls a server process which
Georg Brandl2ee470f2008-07-16 12:55:28 +0000220 holds Python objects and allows other processes to manipulate them using
Benjamin Petersone711caf2008-06-11 16:44:04 +0000221 proxies.
222
223 A manager returned by :func:`Manager` will support types :class:`list`,
224 :class:`dict`, :class:`Namespace`, :class:`Lock`, :class:`RLock`,
225 :class:`Semaphore`, :class:`BoundedSemaphore`, :class:`Condition`,
226 :class:`Event`, :class:`Queue`, :class:`Value` and :class:`Array`. For
227 example, ::
228
229 from multiprocessing import Process, Manager
230
231 def f(d, l):
232 d[1] = '1'
233 d['2'] = 2
234 d[0.25] = None
235 l.reverse()
236
237 if __name__ == '__main__':
238 manager = Manager()
239
240 d = manager.dict()
241 l = manager.list(range(10))
242
243 p = Process(target=f, args=(d, l))
244 p.start()
245 p.join()
246
Georg Brandl49702152008-09-29 06:43:45 +0000247 print(d)
248 print(l)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000249
250 will print ::
251
252 {0.25: None, 1: '1', '2': 2}
253 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
254
255 Server process managers are more flexible than using shared memory objects
256 because they can be made to support arbitrary object types. Also, a single
257 manager can be shared by processes on different computers over a network.
258 They are, however, slower than using shared memory.
259
260
261Using a pool of workers
262~~~~~~~~~~~~~~~~~~~~~~~
263
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000264The :class:`~multiprocessing.pool.Pool` class represents a pool of worker
Benjamin Petersone711caf2008-06-11 16:44:04 +0000265processes. It has methods which allows tasks to be offloaded to the worker
266processes in a few different ways.
267
268For example::
269
270 from multiprocessing import Pool
271
272 def f(x):
273 return x*x
274
275 if __name__ == '__main__':
Jesse Noller45239682008-11-28 18:46:19 +0000276 pool = Pool(processes=4) # start 4 worker processes
277 result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
278 print result.get(timeout=1) # prints "100" unless your computer is *very* slow
279 print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
Benjamin Petersone711caf2008-06-11 16:44:04 +0000280
281
282Reference
283---------
284
285The :mod:`multiprocessing` package mostly replicates the API of the
286:mod:`threading` module.
287
288
289:class:`Process` and exceptions
290~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
292.. class:: Process([group[, target[, name[, args[, kwargs]]]]])
293
294 Process objects represent activity that is run in a separate process. The
295 :class:`Process` class has equivalents of all the methods of
296 :class:`threading.Thread`.
297
298 The constructor should always be called with keyword arguments. *group*
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000299 should always be ``None``; it exists solely for compatibility with
Benjamin Petersona786b022008-08-25 21:05:21 +0000300 :class:`threading.Thread`. *target* is the callable object to be invoked by
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000301 the :meth:`run()` method. It defaults to ``None``, meaning nothing is
Benjamin Petersone711caf2008-06-11 16:44:04 +0000302 called. *name* is the process name. By default, a unique name is constructed
303 of the form 'Process-N\ :sub:`1`:N\ :sub:`2`:...:N\ :sub:`k`' where N\
304 :sub:`1`,N\ :sub:`2`,...,N\ :sub:`k` is a sequence of integers whose length
305 is determined by the *generation* of the process. *args* is the argument
306 tuple for the target invocation. *kwargs* is a dictionary of keyword
307 arguments for the target invocation. By default, no arguments are passed to
308 *target*.
309
310 If a subclass overrides the constructor, it must make sure it invokes the
311 base class constructor (:meth:`Process.__init__`) before doing anything else
312 to the process.
313
314 .. method:: run()
315
316 Method representing the process's activity.
317
318 You may override this method in a subclass. The standard :meth:`run`
319 method invokes the callable object passed to the object's constructor as
320 the target argument, if any, with sequential and keyword arguments taken
321 from the *args* and *kwargs* arguments, respectively.
322
323 .. method:: start()
324
325 Start the process's activity.
326
327 This must be called at most once per process object. It arranges for the
328 object's :meth:`run` method to be invoked in a separate process.
329
330 .. method:: join([timeout])
331
332 Block the calling thread until the process whose :meth:`join` method is
333 called terminates or until the optional timeout occurs.
334
335 If *timeout* is ``None`` then there is no timeout.
336
337 A process can be joined many times.
338
339 A process cannot join itself because this would cause a deadlock. It is
340 an error to attempt to join a process before it has been started.
341
Benjamin Petersona786b022008-08-25 21:05:21 +0000342 .. attribute:: name
Benjamin Petersone711caf2008-06-11 16:44:04 +0000343
Benjamin Petersona786b022008-08-25 21:05:21 +0000344 The process's name.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000345
346 The name is a string used for identification purposes only. It has no
347 semantics. Multiple processes may be given the same name. The initial
348 name is set by the constructor.
349
Jesse Noller45239682008-11-28 18:46:19 +0000350 .. method:: is_alive
Benjamin Petersone711caf2008-06-11 16:44:04 +0000351
352 Return whether the process is alive.
353
354 Roughly, a process object is alive from the moment the :meth:`start`
355 method returns until the child process terminates.
356
Benjamin Petersona786b022008-08-25 21:05:21 +0000357 .. attribute:: daemon
Benjamin Petersone711caf2008-06-11 16:44:04 +0000358
Benjamin Petersona786b022008-08-25 21:05:21 +0000359 The process's daemon flag, a Boolean value. This must be called before
360 :meth:`start` is called.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000361
362 The initial value is inherited from the creating process.
363
364 When a process exits, it attempts to terminate all of its daemonic child
365 processes.
366
367 Note that a daemonic process is not allowed to create child processes.
368 Otherwise a daemonic process would leave its children orphaned if it gets
369 terminated when its parent process exits.
370
Benjamin Petersona786b022008-08-25 21:05:21 +0000371 In addition to the :class:`Threading.Thread` API, :class:`Process` objects
372 also support the following attributes and methods:
Benjamin Petersone711caf2008-06-11 16:44:04 +0000373
Benjamin Petersona786b022008-08-25 21:05:21 +0000374 .. attribute:: pid
Benjamin Petersone711caf2008-06-11 16:44:04 +0000375
376 Return the process ID. Before the process is spawned, this will be
377 ``None``.
378
Benjamin Petersona786b022008-08-25 21:05:21 +0000379 .. attribute:: exitcode
Benjamin Petersone711caf2008-06-11 16:44:04 +0000380
Benjamin Petersona786b022008-08-25 21:05:21 +0000381 The child's exit code. This will be ``None`` if the process has not yet
382 terminated. A negative value *-N* indicates that the child was terminated
383 by signal *N*.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000384
Benjamin Petersona786b022008-08-25 21:05:21 +0000385 .. attribute:: authkey
Benjamin Petersone711caf2008-06-11 16:44:04 +0000386
Benjamin Petersona786b022008-08-25 21:05:21 +0000387 The process's authentication key (a byte string).
Benjamin Petersone711caf2008-06-11 16:44:04 +0000388
389 When :mod:`multiprocessing` is initialized the main process is assigned a
390 random string using :func:`os.random`.
391
392 When a :class:`Process` object is created, it will inherit the
Benjamin Petersona786b022008-08-25 21:05:21 +0000393 authentication key of its parent process, although this may be changed by
394 setting :attr:`authkey` to another byte string.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000395
396 See :ref:`multiprocessing-auth-keys`.
397
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000398 .. method:: terminate()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000399
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000400 Terminate the process. On Unix this is done using the ``SIGTERM`` signal;
401 on Windows :cfunc:`TerminateProcess` is used. Note that exit handlers and
402 finally clauses, etc., will not be executed.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000403
404 Note that descendant processes of the process will *not* be terminated --
405 they will simply become orphaned.
406
407 .. warning::
408
409 If this method is used when the associated process is using a pipe or
410 queue then the pipe or queue is liable to become corrupted and may
411 become unusable by other process. Similarly, if the process has
412 acquired a lock or semaphore etc. then terminating it is liable to
413 cause other processes to deadlock.
414
415 Note that the :meth:`start`, :meth:`join`, :meth:`is_alive` and
Benjamin Petersona786b022008-08-25 21:05:21 +0000416 :attr:`exit_code` methods should only be called by the process that created
417 the process object.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000418
419 Example usage of some of the methods of :class:`Process`::
420
Benjamin Peterson206e3072008-10-19 14:07:49 +0000421 >>> import multiprocessing, time, signal
422 >>> p = multiprocessing.Process(target=time.sleep, args=(1000,))
Georg Brandl49702152008-09-29 06:43:45 +0000423 >>> print(p, p.is_alive())
Benjamin Petersone711caf2008-06-11 16:44:04 +0000424 <Process(Process-1, initial)> False
425 >>> p.start()
Georg Brandl49702152008-09-29 06:43:45 +0000426 >>> print(p, p.is_alive())
Benjamin Petersone711caf2008-06-11 16:44:04 +0000427 <Process(Process-1, started)> True
428 >>> p.terminate()
Georg Brandl49702152008-09-29 06:43:45 +0000429 >>> print(p, p.is_alive())
Benjamin Petersone711caf2008-06-11 16:44:04 +0000430 <Process(Process-1, stopped[SIGTERM])> False
Benjamin Petersona786b022008-08-25 21:05:21 +0000431 >>> p.exitcode == -signal.SIGTERM
Benjamin Petersone711caf2008-06-11 16:44:04 +0000432 True
433
434
435.. exception:: BufferTooShort
436
437 Exception raised by :meth:`Connection.recv_bytes_into()` when the supplied
438 buffer object is too small for the message read.
439
440 If ``e`` is an instance of :exc:`BufferTooShort` then ``e.args[0]`` will give
441 the message as a byte string.
442
443
444Pipes and Queues
445~~~~~~~~~~~~~~~~
446
447When using multiple processes, one generally uses message passing for
448communication between processes and avoids having to use any synchronization
449primitives like locks.
450
451For passing messages one can use :func:`Pipe` (for a connection between two
452processes) or a queue (which allows multiple producers and consumers).
453
454The :class:`Queue` and :class:`JoinableQueue` types are multi-producer,
Benjamin Peterson257060a2008-06-28 01:42:41 +0000455multi-consumer FIFO queues modelled on the :class:`queue.Queue` class in the
Benjamin Petersone711caf2008-06-11 16:44:04 +0000456standard library. They differ in that :class:`Queue` lacks the
Benjamin Peterson257060a2008-06-28 01:42:41 +0000457:meth:`~queue.Queue.task_done` and :meth:`~queue.Queue.join` methods introduced
458into Python 2.5's :class:`queue.Queue` class.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000459
460If you use :class:`JoinableQueue` then you **must** call
461:meth:`JoinableQueue.task_done` for each task removed from the queue or else the
462semaphore used to count the number of unfinished tasks may eventually overflow
463raising an exception.
464
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000465Note that one can also create a shared queue by using a manager object -- see
466:ref:`multiprocessing-managers`.
467
Benjamin Petersone711caf2008-06-11 16:44:04 +0000468.. note::
469
Benjamin Peterson257060a2008-06-28 01:42:41 +0000470 :mod:`multiprocessing` uses the usual :exc:`queue.Empty` and
471 :exc:`queue.Full` exceptions to signal a timeout. They are not available in
Benjamin Petersone711caf2008-06-11 16:44:04 +0000472 the :mod:`multiprocessing` namespace so you need to import them from
Benjamin Peterson257060a2008-06-28 01:42:41 +0000473 :mod:`queue`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000474
475
476.. warning::
477
478 If a process is killed using :meth:`Process.terminate` or :func:`os.kill`
479 while it is trying to use a :class:`Queue`, then the data in the queue is
480 likely to become corrupted. This may cause any other processes to get an
481 exception when it tries to use the queue later on.
482
483.. warning::
484
485 As mentioned above, if a child process has put items on a queue (and it has
486 not used :meth:`JoinableQueue.cancel_join_thread`), then that process will
487 not terminate until all buffered items have been flushed to the pipe.
488
489 This means that if you try joining that process you may get a deadlock unless
490 you are sure that all items which have been put on the queue have been
491 consumed. Similarly, if the child process is non-daemonic then the parent
Georg Brandl2ee470f2008-07-16 12:55:28 +0000492 process may hang on exit when it tries to join all its non-daemonic children.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000493
494 Note that a queue created using a manager does not have this issue. See
495 :ref:`multiprocessing-programming`.
496
Benjamin Petersone711caf2008-06-11 16:44:04 +0000497For an example of the usage of queues for interprocess communication see
498:ref:`multiprocessing-examples`.
499
500
501.. function:: Pipe([duplex])
502
503 Returns a pair ``(conn1, conn2)`` of :class:`Connection` objects representing
504 the ends of a pipe.
505
506 If *duplex* is ``True`` (the default) then the pipe is bidirectional. If
507 *duplex* is ``False`` then the pipe is unidirectional: ``conn1`` can only be
508 used for receiving messages and ``conn2`` can only be used for sending
509 messages.
510
511
512.. class:: Queue([maxsize])
513
514 Returns a process shared queue implemented using a pipe and a few
515 locks/semaphores. When a process first puts an item on the queue a feeder
516 thread is started which transfers objects from a buffer into the pipe.
517
Benjamin Peterson257060a2008-06-28 01:42:41 +0000518 The usual :exc:`queue.Empty` and :exc:`queue.Full` exceptions from the
Benjamin Petersone711caf2008-06-11 16:44:04 +0000519 standard library's :mod:`Queue` module are raised to signal timeouts.
520
Benjamin Peterson257060a2008-06-28 01:42:41 +0000521 :class:`Queue` implements all the methods of :class:`queue.Queue` except for
522 :meth:`~queue.Queue.task_done` and :meth:`~queue.Queue.join`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000523
524 .. method:: qsize()
525
526 Return the approximate size of the queue. Because of
527 multithreading/multiprocessing semantics, this number is not reliable.
528
529 Note that this may raise :exc:`NotImplementedError` on Unix platforms like
Georg Brandlc575c902008-09-13 17:46:05 +0000530 Mac OS X where ``sem_getvalue()`` is not implemented.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000531
532 .. method:: empty()
533
534 Return ``True`` if the queue is empty, ``False`` otherwise. Because of
535 multithreading/multiprocessing semantics, this is not reliable.
536
537 .. method:: full()
538
539 Return ``True`` if the queue is full, ``False`` otherwise. Because of
540 multithreading/multiprocessing semantics, this is not reliable.
541
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000542 .. method:: put(item[, block[, timeout]])
Benjamin Petersone711caf2008-06-11 16:44:04 +0000543
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000544 Put item into the queue. If the optional argument *block* is ``True``
545 (the default) and *timeout* is ``None`` (the default), block if necessary until
Benjamin Petersone711caf2008-06-11 16:44:04 +0000546 a free slot is available. If *timeout* is a positive number, it blocks at
Benjamin Peterson257060a2008-06-28 01:42:41 +0000547 most *timeout* seconds and raises the :exc:`queue.Full` exception if no
Benjamin Petersone711caf2008-06-11 16:44:04 +0000548 free slot was available within that time. Otherwise (*block* is
549 ``False``), put an item on the queue if a free slot is immediately
Benjamin Peterson257060a2008-06-28 01:42:41 +0000550 available, else raise the :exc:`queue.Full` exception (*timeout* is
Benjamin Petersone711caf2008-06-11 16:44:04 +0000551 ignored in that case).
552
553 .. method:: put_nowait(item)
554
555 Equivalent to ``put(item, False)``.
556
557 .. method:: get([block[, timeout]])
558
559 Remove and return an item from the queue. If optional args *block* is
560 ``True`` (the default) and *timeout* is ``None`` (the default), block if
561 necessary until an item is available. If *timeout* is a positive number,
Benjamin Peterson257060a2008-06-28 01:42:41 +0000562 it blocks at most *timeout* seconds and raises the :exc:`queue.Empty`
Benjamin Petersone711caf2008-06-11 16:44:04 +0000563 exception if no item was available within that time. Otherwise (block is
564 ``False``), return an item if one is immediately available, else raise the
Benjamin Peterson257060a2008-06-28 01:42:41 +0000565 :exc:`queue.Empty` exception (*timeout* is ignored in that case).
Benjamin Petersone711caf2008-06-11 16:44:04 +0000566
567 .. method:: get_nowait()
568 get_no_wait()
569
570 Equivalent to ``get(False)``.
571
572 :class:`multiprocessing.Queue` has a few additional methods not found in
Georg Brandl2ee470f2008-07-16 12:55:28 +0000573 :class:`queue.Queue`. These methods are usually unnecessary for most
574 code:
Benjamin Petersone711caf2008-06-11 16:44:04 +0000575
576 .. method:: close()
577
578 Indicate that no more data will be put on this queue by the current
579 process. The background thread will quit once it has flushed all buffered
580 data to the pipe. This is called automatically when the queue is garbage
581 collected.
582
583 .. method:: join_thread()
584
585 Join the background thread. This can only be used after :meth:`close` has
586 been called. It blocks until the background thread exits, ensuring that
587 all data in the buffer has been flushed to the pipe.
588
589 By default if a process is not the creator of the queue then on exit it
590 will attempt to join the queue's background thread. The process can call
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000591 :meth:`cancel_join_thread` to make :meth:`join_thread` do nothing.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000592
593 .. method:: cancel_join_thread()
594
595 Prevent :meth:`join_thread` from blocking. In particular, this prevents
596 the background thread from being joined automatically when the process
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000597 exits -- see :meth:`join_thread`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000598
599
600.. class:: JoinableQueue([maxsize])
601
602 :class:`JoinableQueue`, a :class:`Queue` subclass, is a queue which
603 additionally has :meth:`task_done` and :meth:`join` methods.
604
605 .. method:: task_done()
606
607 Indicate that a formerly enqueued task is complete. Used by queue consumer
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000608 threads. For each :meth:`~Queue.get` used to fetch a task, a subsequent
609 call to :meth:`task_done` tells the queue that the processing on the task
610 is complete.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000611
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000612 If a :meth:`~Queue.join` is currently blocking, it will resume when all
613 items have been processed (meaning that a :meth:`task_done` call was
614 received for every item that had been :meth:`~Queue.put` into the queue).
Benjamin Petersone711caf2008-06-11 16:44:04 +0000615
616 Raises a :exc:`ValueError` if called more times than there were items
617 placed in the queue.
618
619
620 .. method:: join()
621
622 Block until all items in the queue have been gotten and processed.
623
624 The count of unfinished tasks goes up whenever an item is added to the
625 queue. The count goes down whenever a consumer thread calls
626 :meth:`task_done` to indicate that the item was retrieved and all work on
627 it is complete. When the count of unfinished tasks drops to zero,
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000628 :meth:`~Queue.join` unblocks.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000629
630
631Miscellaneous
632~~~~~~~~~~~~~
633
634.. function:: active_children()
635
636 Return list of all live children of the current process.
637
638 Calling this has the side affect of "joining" any processes which have
639 already finished.
640
641.. function:: cpu_count()
642
643 Return the number of CPUs in the system. May raise
644 :exc:`NotImplementedError`.
645
646.. function:: current_process()
647
648 Return the :class:`Process` object corresponding to the current process.
649
650 An analogue of :func:`threading.current_thread`.
651
652.. function:: freeze_support()
653
654 Add support for when a program which uses :mod:`multiprocessing` has been
655 frozen to produce a Windows executable. (Has been tested with **py2exe**,
656 **PyInstaller** and **cx_Freeze**.)
657
658 One needs to call this function straight after the ``if __name__ ==
659 '__main__'`` line of the main module. For example::
660
661 from multiprocessing import Process, freeze_support
662
663 def f():
Georg Brandl49702152008-09-29 06:43:45 +0000664 print('hello world!')
Benjamin Petersone711caf2008-06-11 16:44:04 +0000665
666 if __name__ == '__main__':
667 freeze_support()
668 Process(target=f).start()
669
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000670 If the ``freeze_support()`` line is missed out then trying to run the frozen
671 executable will raise :exc:`RuntimeError`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000672
673 If the module is being run normally by the Python interpreter then
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000674 :func:`freeze_support` has no effect.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000675
676.. function:: set_executable()
677
678 Sets the path of the python interpreter to use when starting a child process.
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000679 (By default :data:`sys.executable` is used). Embedders will probably need to
680 do some thing like ::
Benjamin Petersone711caf2008-06-11 16:44:04 +0000681
682 setExecutable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
683
684 before they can create child processes. (Windows only)
685
686
687.. note::
688
689 :mod:`multiprocessing` contains no analogues of
690 :func:`threading.active_count`, :func:`threading.enumerate`,
691 :func:`threading.settrace`, :func:`threading.setprofile`,
692 :class:`threading.Timer`, or :class:`threading.local`.
693
694
695Connection Objects
696~~~~~~~~~~~~~~~~~~
697
698Connection objects allow the sending and receiving of picklable objects or
699strings. They can be thought of as message oriented connected sockets.
700
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000701Connection objects usually created using :func:`Pipe` -- see also
Benjamin Petersone711caf2008-06-11 16:44:04 +0000702:ref:`multiprocessing-listeners-clients`.
703
704.. class:: Connection
705
706 .. method:: send(obj)
707
708 Send an object to the other end of the connection which should be read
709 using :meth:`recv`.
710
711 The object must be picklable.
712
713 .. method:: recv()
714
715 Return an object sent from the other end of the connection using
716 :meth:`send`. Raises :exc:`EOFError` if there is nothing left to receive
717 and the other end was closed.
718
719 .. method:: fileno()
720
721 Returns the file descriptor or handle used by the connection.
722
723 .. method:: close()
724
725 Close the connection.
726
727 This is called automatically when the connection is garbage collected.
728
729 .. method:: poll([timeout])
730
731 Return whether there is any data available to be read.
732
733 If *timeout* is not specified then it will return immediately. If
734 *timeout* is a number then this specifies the maximum time in seconds to
735 block. If *timeout* is ``None`` then an infinite timeout is used.
736
737 .. method:: send_bytes(buffer[, offset[, size]])
738
739 Send byte data from an object supporting the buffer interface as a
740 complete message.
741
742 If *offset* is given then data is read from that position in *buffer*. If
743 *size* is given then that many bytes will be read from buffer.
744
745 .. method:: recv_bytes([maxlength])
746
747 Return a complete message of byte data sent from the other end of the
748 connection as a string. Raises :exc:`EOFError` if there is nothing left
749 to receive and the other end has closed.
750
751 If *maxlength* is specified and the message is longer than *maxlength*
752 then :exc:`IOError` is raised and the connection will no longer be
753 readable.
754
755 .. method:: recv_bytes_into(buffer[, offset])
756
757 Read into *buffer* a complete message of byte data sent from the other end
758 of the connection and return the number of bytes in the message. Raises
759 :exc:`EOFError` if there is nothing left to receive and the other end was
760 closed.
761
762 *buffer* must be an object satisfying the writable buffer interface. If
763 *offset* is given then the message will be written into the buffer from
764 *that position. Offset must be a non-negative integer less than the
765 *length of *buffer* (in bytes).
766
767 If the buffer is too short then a :exc:`BufferTooShort` exception is
768 raised and the complete message is available as ``e.args[0]`` where ``e``
769 is the exception instance.
770
771
772For example:
773
774 >>> from multiprocessing import Pipe
775 >>> a, b = Pipe()
776 >>> a.send([1, 'hello', None])
777 >>> b.recv()
778 [1, 'hello', None]
779 >>> b.send_bytes('thank you')
780 >>> a.recv_bytes()
781 'thank you'
782 >>> import array
783 >>> arr1 = array.array('i', range(5))
784 >>> arr2 = array.array('i', [0] * 10)
785 >>> a.send_bytes(arr1)
786 >>> count = b.recv_bytes_into(arr2)
787 >>> assert count == len(arr1) * arr1.itemsize
788 >>> arr2
789 array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])
790
791
792.. warning::
793
794 The :meth:`Connection.recv` method automatically unpickles the data it
795 receives, which can be a security risk unless you can trust the process
796 which sent the message.
797
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000798 Therefore, unless the connection object was produced using :func:`Pipe` you
799 should only use the :meth:`~Connection.recv` and :meth:`~Connection.send`
800 methods after performing some sort of authentication. See
801 :ref:`multiprocessing-auth-keys`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000802
803.. warning::
804
805 If a process is killed while it is trying to read or write to a pipe then
806 the data in the pipe is likely to become corrupted, because it may become
807 impossible to be sure where the message boundaries lie.
808
809
810Synchronization primitives
811~~~~~~~~~~~~~~~~~~~~~~~~~~
812
813Generally synchronization primitives are not as necessary in a multiprocess
Georg Brandl2ee470f2008-07-16 12:55:28 +0000814program as they are in a multithreaded program. See the documentation for
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000815:mod:`threading` module.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000816
817Note that one can also create synchronization primitives by using a manager
818object -- see :ref:`multiprocessing-managers`.
819
820.. class:: BoundedSemaphore([value])
821
822 A bounded semaphore object: a clone of :class:`threading.BoundedSemaphore`.
823
Georg Brandlc575c902008-09-13 17:46:05 +0000824 (On Mac OS X this is indistinguishable from :class:`Semaphore` because
Benjamin Petersone711caf2008-06-11 16:44:04 +0000825 ``sem_getvalue()`` is not implemented on that platform).
826
827.. class:: Condition([lock])
828
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000829 A condition variable: a clone of :class:`threading.Condition`.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000830
831 If *lock* is specified then it should be a :class:`Lock` or :class:`RLock`
832 object from :mod:`multiprocessing`.
833
834.. class:: Event()
835
836 A clone of :class:`threading.Event`.
837
838.. class:: Lock()
839
840 A non-recursive lock object: a clone of :class:`threading.Lock`.
841
842.. class:: RLock()
843
844 A recursive lock object: a clone of :class:`threading.RLock`.
845
846.. class:: Semaphore([value])
847
848 A bounded semaphore object: a clone of :class:`threading.Semaphore`.
849
850.. note::
851
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000852 The :meth:`acquire` method of :class:`BoundedSemaphore`, :class:`Lock`,
Benjamin Petersone711caf2008-06-11 16:44:04 +0000853 :class:`RLock` and :class:`Semaphore` has a timeout parameter not supported
854 by the equivalents in :mod:`threading`. The signature is
855 ``acquire(block=True, timeout=None)`` with keyword parameters being
856 acceptable. If *block* is ``True`` and *timeout* is not ``None`` then it
857 specifies a timeout in seconds. If *block* is ``False`` then *timeout* is
858 ignored.
Jesse Noller45239682008-11-28 18:46:19 +0000859
860 Note that on OS/X ``sem_timedwait`` is unsupported, so timeout arguments
861 for these will be ignored.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000862
863.. note::
864
865 If the SIGINT signal generated by Ctrl-C arrives while the main thread is
866 blocked by a call to :meth:`BoundedSemaphore.acquire`, :meth:`Lock.acquire`,
867 :meth:`RLock.acquire`, :meth:`Semaphore.acquire`, :meth:`Condition.acquire`
868 or :meth:`Condition.wait` then the call will be immediately interrupted and
869 :exc:`KeyboardInterrupt` will be raised.
870
871 This differs from the behaviour of :mod:`threading` where SIGINT will be
872 ignored while the equivalent blocking calls are in progress.
873
874
875Shared :mod:`ctypes` Objects
876~~~~~~~~~~~~~~~~~~~~~~~~~~~~
877
878It is possible to create shared objects using shared memory which can be
879inherited by child processes.
880
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000881.. function:: Value(typecode_or_type[, *args, lock]])
Benjamin Petersone711caf2008-06-11 16:44:04 +0000882
883 Return a :mod:`ctypes` object allocated from shared memory. By default the
884 return value is actually a synchronized wrapper for the object.
885
886 *typecode_or_type* determines the type of the returned object: it is either a
887 ctypes type or a one character typecode of the kind used by the :mod:`array`
888 module. *\*args* is passed on to the constructor for the type.
889
890 If *lock* is ``True`` (the default) then a new lock object is created to
891 synchronize access to the value. If *lock* is a :class:`Lock` or
892 :class:`RLock` object then that will be used to synchronize access to the
893 value. If *lock* is ``False`` then access to the returned object will not be
894 automatically protected by a lock, so it will not necessarily be
895 "process-safe".
896
897 Note that *lock* is a keyword-only argument.
898
899.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True)
900
901 Return a ctypes array allocated from shared memory. By default the return
902 value is actually a synchronized wrapper for the array.
903
904 *typecode_or_type* determines the type of the elements of the returned array:
905 it is either a ctypes type or a one character typecode of the kind used by
906 the :mod:`array` module. If *size_or_initializer* is an integer, then it
907 determines the length of the array, and the array will be initially zeroed.
908 Otherwise, *size_or_initializer* is a sequence which is used to initialize
909 the array and whose length determines the length of the array.
910
911 If *lock* is ``True`` (the default) then a new lock object is created to
912 synchronize access to the value. If *lock* is a :class:`Lock` or
913 :class:`RLock` object then that will be used to synchronize access to the
914 value. If *lock* is ``False`` then access to the returned object will not be
915 automatically protected by a lock, so it will not necessarily be
916 "process-safe".
917
918 Note that *lock* is a keyword only argument.
919
Amaury Forgeot d'Arcb0c29162008-11-22 22:18:04 +0000920 Note that an array of :data:`ctypes.c_char` has *value* and *raw*
Benjamin Petersone711caf2008-06-11 16:44:04 +0000921 attributes which allow one to use it to store and retrieve strings.
922
923
924The :mod:`multiprocessing.sharedctypes` module
925>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
926
927.. module:: multiprocessing.sharedctypes
928 :synopsis: Allocate ctypes objects from shared memory.
929
930The :mod:`multiprocessing.sharedctypes` module provides functions for allocating
931:mod:`ctypes` objects from shared memory which can be inherited by child
932processes.
933
934.. note::
935
Georg Brandl2ee470f2008-07-16 12:55:28 +0000936 Although it is possible to store a pointer in shared memory remember that
937 this will refer to a location in the address space of a specific process.
Benjamin Petersone711caf2008-06-11 16:44:04 +0000938 However, the pointer is quite likely to be invalid in the context of a second
939 process and trying to dereference the pointer from the second process may
940 cause a crash.
941
942.. function:: RawArray(typecode_or_type, size_or_initializer)
943
944 Return a ctypes array allocated from shared memory.
945
946 *typecode_or_type* determines the type of the elements of the returned array:
947 it is either a ctypes type or a one character typecode of the kind used by
948 the :mod:`array` module. If *size_or_initializer* is an integer then it
949 determines the length of the array, and the array will be initially zeroed.
950 Otherwise *size_or_initializer* is a sequence which is used to initialize the
951 array and whose length determines the length of the array.
952
953 Note that setting and getting an element is potentially non-atomic -- use
954 :func:`Array` instead to make sure that access is automatically synchronized
955 using a lock.
956
957.. function:: RawValue(typecode_or_type, *args)
958
959 Return a ctypes object allocated from shared memory.
960
961 *typecode_or_type* determines the type of the returned object: it is either a
962 ctypes type or a one character typecode of the kind used by the :mod:`array`
963 module. */*args* is passed on to the constructor for the type.
964
965 Note that setting and getting the value is potentially non-atomic -- use
966 :func:`Value` instead to make sure that access is automatically synchronized
967 using a lock.
968
Amaury Forgeot d'Arcb0c29162008-11-22 22:18:04 +0000969 Note that an array of :data:`ctypes.c_char` has ``value`` and ``raw``
Benjamin Petersone711caf2008-06-11 16:44:04 +0000970 attributes which allow one to use it to store and retrieve strings -- see
971 documentation for :mod:`ctypes`.
972
Benjamin Peterson5289b2b2008-06-28 00:40:54 +0000973.. function:: Array(typecode_or_type, size_or_initializer[, *args[, lock]])
Benjamin Petersone711caf2008-06-11 16:44:04 +0000974
975 The same as :func:`RawArray` except that depending on the value of *lock* a
976 process-safe synchronization wrapper may be returned instead of a raw ctypes
977 array.
978
979 If *lock* is ``True`` (the default) then a new lock object is created to
980 synchronize access to the value. If *lock* is a :class:`Lock` or
981 :class:`RLock` object then that will be used to synchronize access to the
982 value. If *lock* is ``False`` then access to the returned object will not be
983 automatically protected by a lock, so it will not necessarily be
984 "process-safe".
985
986 Note that *lock* is a keyword-only argument.
987
988.. function:: Value(typecode_or_type, *args[, lock])
989
990 The same as :func:`RawValue` except that depending on the value of *lock* a
991 process-safe synchronization wrapper may be returned instead of a raw ctypes
992 object.
993
994 If *lock* is ``True`` (the default) then a new lock object is created to
995 synchronize access to the value. If *lock* is a :class:`Lock` or
996 :class:`RLock` object then that will be used to synchronize access to the
997 value. If *lock* is ``False`` then access to the returned object will not be
998 automatically protected by a lock, so it will not necessarily be
999 "process-safe".
1000
1001 Note that *lock* is a keyword-only argument.
1002
1003.. function:: copy(obj)
1004
1005 Return a ctypes object allocated from shared memory which is a copy of the
1006 ctypes object *obj*.
1007
1008.. function:: synchronized(obj[, lock])
1009
1010 Return a process-safe wrapper object for a ctypes object which uses *lock* to
1011 synchronize access. If *lock* is ``None`` (the default) then a
1012 :class:`multiprocessing.RLock` object is created automatically.
1013
1014 A synchronized wrapper will have two methods in addition to those of the
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001015 object it wraps: :meth:`get_obj` returns the wrapped object and
1016 :meth:`get_lock` returns the lock object used for synchronization.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001017
1018 Note that accessing the ctypes object through the wrapper can be a lot slower
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001019 than accessing the raw ctypes object.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001020
1021
1022The table below compares the syntax for creating shared ctypes objects from
1023shared memory with the normal ctypes syntax. (In the table ``MyStruct`` is some
1024subclass of :class:`ctypes.Structure`.)
1025
1026==================== ========================== ===========================
1027ctypes sharedctypes using type sharedctypes using typecode
1028==================== ========================== ===========================
1029c_double(2.4) RawValue(c_double, 2.4) RawValue('d', 2.4)
1030MyStruct(4, 6) RawValue(MyStruct, 4, 6)
1031(c_short * 7)() RawArray(c_short, 7) RawArray('h', 7)
1032(c_int * 3)(9, 2, 8) RawArray(c_int, (9, 2, 8)) RawArray('i', (9, 2, 8))
1033==================== ========================== ===========================
1034
1035
1036Below is an example where a number of ctypes objects are modified by a child
1037process::
1038
1039 from multiprocessing import Process, Lock
1040 from multiprocessing.sharedctypes import Value, Array
1041 from ctypes import Structure, c_double
1042
1043 class Point(Structure):
1044 _fields_ = [('x', c_double), ('y', c_double)]
1045
1046 def modify(n, x, s, A):
1047 n.value **= 2
1048 x.value **= 2
1049 s.value = s.value.upper()
1050 for a in A:
1051 a.x **= 2
1052 a.y **= 2
1053
1054 if __name__ == '__main__':
1055 lock = Lock()
1056
1057 n = Value('i', 7)
1058 x = Value(ctypes.c_double, 1.0/3.0, lock=False)
1059 s = Array('c', 'hello world', lock=lock)
1060 A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)
1061
1062 p = Process(target=modify, args=(n, x, s, A))
1063 p.start()
1064 p.join()
1065
Georg Brandl49702152008-09-29 06:43:45 +00001066 print(n.value)
1067 print(x.value)
1068 print(s.value)
1069 print([(a.x, a.y) for a in A])
Benjamin Petersone711caf2008-06-11 16:44:04 +00001070
1071
Georg Brandl49702152008-09-29 06:43:45 +00001072.. highlight:: none
Benjamin Petersone711caf2008-06-11 16:44:04 +00001073
1074The results printed are ::
1075
1076 49
1077 0.1111111111111111
1078 HELLO WORLD
1079 [(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]
1080
Georg Brandl49702152008-09-29 06:43:45 +00001081.. highlight:: python
Benjamin Petersone711caf2008-06-11 16:44:04 +00001082
1083
1084.. _multiprocessing-managers:
1085
1086Managers
1087~~~~~~~~
1088
1089Managers provide a way to create data which can be shared between different
1090processes. A manager object controls a server process which manages *shared
1091objects*. Other processes can access the shared objects by using proxies.
1092
1093.. function:: multiprocessing.Manager()
1094
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001095 Returns a started :class:`~multiprocessing.managers.SyncManager` object which
1096 can be used for sharing objects between processes. The returned manager
1097 object corresponds to a spawned child process and has methods which will
1098 create shared objects and return corresponding proxies.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001099
1100.. module:: multiprocessing.managers
1101 :synopsis: Share data between process with shared objects.
1102
1103Manager processes will be shutdown as soon as they are garbage collected or
1104their parent process exits. The manager classes are defined in the
1105:mod:`multiprocessing.managers` module:
1106
1107.. class:: BaseManager([address[, authkey]])
1108
1109 Create a BaseManager object.
1110
1111 Once created one should call :meth:`start` or :meth:`serve_forever` to ensure
1112 that the manager object refers to a started manager process.
1113
1114 *address* is the address on which the manager process listens for new
1115 connections. If *address* is ``None`` then an arbitrary one is chosen.
1116
1117 *authkey* is the authentication key which will be used to check the validity
1118 of incoming connections to the server process. If *authkey* is ``None`` then
Benjamin Petersona786b022008-08-25 21:05:21 +00001119 ``current_process().authkey``. Otherwise *authkey* is used and it
Benjamin Petersone711caf2008-06-11 16:44:04 +00001120 must be a string.
1121
1122 .. method:: start()
1123
1124 Start a subprocess to start the manager.
1125
Georg Brandl2ee470f2008-07-16 12:55:28 +00001126 .. method:: serve_forever()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001127
1128 Run the server in the current process.
1129
1130 .. method:: from_address(address, authkey)
1131
1132 A class method which creates a manager object referring to a pre-existing
1133 server process which is using the given address and authentication key.
1134
Jesse Noller45239682008-11-28 18:46:19 +00001135 .. method:: get_server()
1136
1137 Returns a :class:`Server` object which represents the actual server under
1138 the control of the Manager. The :class:`Server` object supports the
1139 :meth:`serve_forever` method::
1140
1141 >>> from multiprocessing.managers import BaseManager
1142 >>> m = BaseManager(address=('', 50000), authkey='abc'))
1143 >>> server = m.get_server()
1144 >>> s.serve_forever()
1145
1146 :class:`Server` additionally have an :attr:`address` attribute.
1147
1148 .. method:: connect()
1149
1150 Connect a local manager object to a remote manager process::
1151
1152 >>> from multiprocessing.managers import BaseManager
1153 >>> m = BaseManager(address='127.0.0.1', authkey='abc))
1154 >>> m.connect()
1155
Benjamin Petersone711caf2008-06-11 16:44:04 +00001156 .. method:: shutdown()
1157
1158 Stop the process used by the manager. This is only available if
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001159 :meth:`start` has been used to start the server process.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001160
1161 This can be called multiple times.
1162
1163 .. method:: register(typeid[, callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]])
1164
1165 A classmethod which can be used for registering a type or callable with
1166 the manager class.
1167
1168 *typeid* is a "type identifier" which is used to identify a particular
1169 type of shared object. This must be a string.
1170
1171 *callable* is a callable used for creating objects for this type
1172 identifier. If a manager instance will be created using the
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001173 :meth:`from_address` classmethod or if the *create_method* argument is
Benjamin Petersone711caf2008-06-11 16:44:04 +00001174 ``False`` then this can be left as ``None``.
1175
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001176 *proxytype* is a subclass of :class:`BaseProxy` which is used to create
1177 proxies for shared objects with this *typeid*. If ``None`` then a proxy
1178 class is created automatically.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001179
1180 *exposed* is used to specify a sequence of method names which proxies for
1181 this typeid should be allowed to access using
1182 :meth:`BaseProxy._callMethod`. (If *exposed* is ``None`` then
1183 :attr:`proxytype._exposed_` is used instead if it exists.) In the case
1184 where no exposed list is specified, all "public methods" of the shared
1185 object will be accessible. (Here a "public method" means any attribute
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001186 which has a :meth:`__call__` method and whose name does not begin with
Benjamin Petersone711caf2008-06-11 16:44:04 +00001187 ``'_'``.)
1188
1189 *method_to_typeid* is a mapping used to specify the return type of those
1190 exposed methods which should return a proxy. It maps method names to
1191 typeid strings. (If *method_to_typeid* is ``None`` then
1192 :attr:`proxytype._method_to_typeid_` is used instead if it exists.) If a
1193 method's name is not a key of this mapping or if the mapping is ``None``
1194 then the object returned by the method will be copied by value.
1195
1196 *create_method* determines whether a method should be created with name
1197 *typeid* which can be used to tell the server process to create a new
1198 shared object and return a proxy for it. By default it is ``True``.
1199
1200 :class:`BaseManager` instances also have one read-only property:
1201
1202 .. attribute:: address
1203
1204 The address used by the manager.
1205
1206
1207.. class:: SyncManager
1208
1209 A subclass of :class:`BaseManager` which can be used for the synchronization
1210 of processes. Objects of this type are returned by
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001211 :func:`multiprocessing.Manager`.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001212
1213 It also supports creation of shared lists and dictionaries.
1214
1215 .. method:: BoundedSemaphore([value])
1216
1217 Create a shared :class:`threading.BoundedSemaphore` object and return a
1218 proxy for it.
1219
1220 .. method:: Condition([lock])
1221
1222 Create a shared :class:`threading.Condition` object and return a proxy for
1223 it.
1224
1225 If *lock* is supplied then it should be a proxy for a
1226 :class:`threading.Lock` or :class:`threading.RLock` object.
1227
1228 .. method:: Event()
1229
1230 Create a shared :class:`threading.Event` object and return a proxy for it.
1231
1232 .. method:: Lock()
1233
1234 Create a shared :class:`threading.Lock` object and return a proxy for it.
1235
1236 .. method:: Namespace()
1237
1238 Create a shared :class:`Namespace` object and return a proxy for it.
1239
1240 .. method:: Queue([maxsize])
1241
Benjamin Peterson257060a2008-06-28 01:42:41 +00001242 Create a shared :class:`queue.Queue` object and return a proxy for it.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001243
1244 .. method:: RLock()
1245
1246 Create a shared :class:`threading.RLock` object and return a proxy for it.
1247
1248 .. method:: Semaphore([value])
1249
1250 Create a shared :class:`threading.Semaphore` object and return a proxy for
1251 it.
1252
1253 .. method:: Array(typecode, sequence)
1254
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001255 Create an array and return a proxy for it.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001256
1257 .. method:: Value(typecode, value)
1258
1259 Create an object with a writable ``value`` attribute and return a proxy
1260 for it.
1261
1262 .. method:: dict()
1263 dict(mapping)
1264 dict(sequence)
1265
1266 Create a shared ``dict`` object and return a proxy for it.
1267
1268 .. method:: list()
1269 list(sequence)
1270
1271 Create a shared ``list`` object and return a proxy for it.
1272
1273
1274Namespace objects
1275>>>>>>>>>>>>>>>>>
1276
1277A namespace object has no public methods, but does have writable attributes.
1278Its representation shows the values of its attributes.
1279
1280However, when using a proxy for a namespace object, an attribute beginning with
1281``'_'`` will be an attribute of the proxy and not an attribute of the referent::
1282
1283 >>> manager = multiprocessing.Manager()
1284 >>> Global = manager.Namespace()
1285 >>> Global.x = 10
1286 >>> Global.y = 'hello'
1287 >>> Global._z = 12.3 # this is an attribute of the proxy
Georg Brandl49702152008-09-29 06:43:45 +00001288 >>> print(Global)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001289 Namespace(x=10, y='hello')
1290
1291
1292Customized managers
1293>>>>>>>>>>>>>>>>>>>
1294
1295To create one's own manager, one creates a subclass of :class:`BaseManager` and
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001296use the :meth:`~BaseManager.resgister` classmethod to register new types or
1297callables with the manager class. For example::
Benjamin Petersone711caf2008-06-11 16:44:04 +00001298
1299 from multiprocessing.managers import BaseManager
1300
1301 class MathsClass(object):
1302 def add(self, x, y):
1303 return x + y
1304 def mul(self, x, y):
1305 return x * y
1306
1307 class MyManager(BaseManager):
1308 pass
1309
1310 MyManager.register('Maths', MathsClass)
1311
1312 if __name__ == '__main__':
1313 manager = MyManager()
1314 manager.start()
1315 maths = manager.Maths()
Georg Brandl49702152008-09-29 06:43:45 +00001316 print(maths.add(4, 3)) # prints 7
1317 print(maths.mul(7, 8)) # prints 56
Benjamin Petersone711caf2008-06-11 16:44:04 +00001318
1319
1320Using a remote manager
1321>>>>>>>>>>>>>>>>>>>>>>
1322
1323It is possible to run a manager server on one machine and have clients use it
1324from other machines (assuming that the firewalls involved allow it).
1325
1326Running the following commands creates a server for a single shared queue which
1327remote clients can access::
1328
1329 >>> from multiprocessing.managers import BaseManager
Benjamin Peterson257060a2008-06-28 01:42:41 +00001330 >>> import queue
1331 >>> queue = queue.Queue()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001332 >>> class QueueManager(BaseManager): pass
1333 ...
Jesse Noller45239682008-11-28 18:46:19 +00001334 >>> QueueManager.register('get_queue', callable=lambda:queue)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001335 >>> m = QueueManager(address=('', 50000), authkey='abracadabra')
Jesse Noller45239682008-11-28 18:46:19 +00001336 >>> s = m.get_server()
1337 >>> s.serveForever()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001338
1339One client can access the server as follows::
1340
1341 >>> from multiprocessing.managers import BaseManager
1342 >>> class QueueManager(BaseManager): pass
1343 ...
Jesse Noller45239682008-11-28 18:46:19 +00001344 >>> QueueManager.register('get_queue')
1345 >>> m = QueueManager(address=('foo.bar.org', 50000), authkey='abracadabra')
1346 >>> m.connect()
1347 >>> queue = m.get_queue()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001348 >>> queue.put('hello')
1349
1350Another client can also use it::
1351
1352 >>> from multiprocessing.managers import BaseManager
1353 >>> class QueueManager(BaseManager): pass
1354 ...
1355 >>> QueueManager.register('getQueue')
1356 >>> m = QueueManager.from_address(address=('foo.bar.org', 50000), authkey='abracadabra')
1357 >>> queue = m.getQueue()
1358 >>> queue.get()
1359 'hello'
1360
Jesse Noller45239682008-11-28 18:46:19 +00001361Local processes can also access that queue, using the code from above on the
1362client to access it remotely::
1363
1364 >>> from multiprocessing import Process, Queue
1365 >>> from multiprocessing.managers import BaseManager
1366 >>> class Worker(Process):
1367 ... def __init__(self, q):
1368 ... self.q = q
1369 ... super(Worker, self).__init__()
1370 ... def run(self):
1371 ... self.q.put('local hello')
1372 ...
1373 >>> queue = Queue()
1374 >>> w = Worker(queue)
1375 >>> w.start()
1376 >>> class QueueManager(BaseManager): pass
1377 ...
1378 >>> QueueManager.register('get_queue', callable=lambda: queue)
1379 >>> m = QueueManager(address=('', 50000), authkey='abracadabra')
1380 >>> s = m.get_server()
1381 >>> s.serve_forever()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001382
1383Proxy Objects
1384~~~~~~~~~~~~~
1385
1386A proxy is an object which *refers* to a shared object which lives (presumably)
1387in a different process. The shared object is said to be the *referent* of the
1388proxy. Multiple proxy objects may have the same referent.
1389
1390A proxy object has methods which invoke corresponding methods of its referent
1391(although not every method of the referent will necessarily be available through
1392the proxy). A proxy can usually be used in most of the same ways that its
1393referent can::
1394
1395 >>> from multiprocessing import Manager
1396 >>> manager = Manager()
1397 >>> l = manager.list([i*i for i in range(10)])
Georg Brandl49702152008-09-29 06:43:45 +00001398 >>> print(l)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001399 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Georg Brandl49702152008-09-29 06:43:45 +00001400 >>> print(repr(l))
Benjamin Petersone711caf2008-06-11 16:44:04 +00001401 <ListProxy object, typeid 'list' at 0xb799974c>
1402 >>> l[4]
1403 16
1404 >>> l[2:5]
1405 [4, 9, 16]
1406
1407Notice that applying :func:`str` to a proxy will return the representation of
1408the referent, whereas applying :func:`repr` will return the representation of
1409the proxy.
1410
1411An important feature of proxy objects is that they are picklable so they can be
1412passed between processes. Note, however, that if a proxy is sent to the
1413corresponding manager's process then unpickling it will produce the referent
1414itself. This means, for example, that one shared object can contain a second::
1415
1416 >>> a = manager.list()
1417 >>> b = manager.list()
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001418 >>> a.append(b) # referent of a now contains referent of b
Georg Brandl49702152008-09-29 06:43:45 +00001419 >>> print(a, b)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001420 [[]] []
1421 >>> b.append('hello')
Georg Brandl49702152008-09-29 06:43:45 +00001422 >>> print(a, b)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001423 [['hello']] ['hello']
1424
1425.. note::
1426
1427 The proxy types in :mod:`multiprocessing` do nothing to support comparisons
1428 by value. So, for instance, ::
1429
1430 manager.list([1,2,3]) == [1,2,3]
1431
1432 will return ``False``. One should just use a copy of the referent instead
1433 when making comparisons.
1434
1435.. class:: BaseProxy
1436
1437 Proxy objects are instances of subclasses of :class:`BaseProxy`.
1438
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001439 .. method:: _callmethod(methodname[, args[, kwds]])
Benjamin Petersone711caf2008-06-11 16:44:04 +00001440
1441 Call and return the result of a method of the proxy's referent.
1442
1443 If ``proxy`` is a proxy whose referent is ``obj`` then the expression ::
1444
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001445 proxy._callmethod(methodname, args, kwds)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001446
1447 will evaluate the expression ::
1448
1449 getattr(obj, methodname)(*args, **kwds)
1450
1451 in the manager's process.
1452
1453 The returned value will be a copy of the result of the call or a proxy to
1454 a new shared object -- see documentation for the *method_to_typeid*
1455 argument of :meth:`BaseManager.register`.
1456
1457 If an exception is raised by the call, then then is re-raised by
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001458 :meth:`_callmethod`. If some other exception is raised in the manager's
Benjamin Petersone711caf2008-06-11 16:44:04 +00001459 process then this is converted into a :exc:`RemoteError` exception and is
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001460 raised by :meth:`_callmethod`.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001461
1462 Note in particular that an exception will be raised if *methodname* has
1463 not been *exposed*
1464
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001465 An example of the usage of :meth:`_callmethod`::
Benjamin Petersone711caf2008-06-11 16:44:04 +00001466
1467 >>> l = manager.list(range(10))
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001468 >>> l._callmethod('__len__')
Benjamin Petersone711caf2008-06-11 16:44:04 +00001469 10
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001470 >>> l._callmethod('__getslice__', (2, 7)) # equiv to `l[2:7]`
Benjamin Petersone711caf2008-06-11 16:44:04 +00001471 [2, 3, 4, 5, 6]
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001472 >>> l._callmethod('__getitem__', (20,)) # equiv to `l[20]`
Benjamin Petersone711caf2008-06-11 16:44:04 +00001473 Traceback (most recent call last):
1474 ...
1475 IndexError: list index out of range
1476
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +00001477 .. method:: _getvalue()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001478
1479 Return a copy of the referent.
1480
1481 If the referent is unpicklable then this will raise an exception.
1482
1483 .. method:: __repr__
1484
1485 Return a representation of the proxy object.
1486
1487 .. method:: __str__
1488
1489 Return the representation of the referent.
1490
1491
1492Cleanup
1493>>>>>>>
1494
1495A proxy object uses a weakref callback so that when it gets garbage collected it
1496deregisters itself from the manager which owns its referent.
1497
1498A shared object gets deleted from the manager process when there are no longer
1499any proxies referring to it.
1500
1501
1502Process Pools
1503~~~~~~~~~~~~~
1504
1505.. module:: multiprocessing.pool
1506 :synopsis: Create pools of processes.
1507
1508One can create a pool of processes which will carry out tasks submitted to it
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001509with the :class:`Pool` class.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001510
1511.. class:: multiprocessing.Pool([processes[, initializer[, initargs]]])
1512
1513 A process pool object which controls a pool of worker processes to which jobs
1514 can be submitted. It supports asynchronous results with timeouts and
1515 callbacks and has a parallel map implementation.
1516
1517 *processes* is the number of worker processes to use. If *processes* is
1518 ``None`` then the number returned by :func:`cpu_count` is used. If
1519 *initializer* is not ``None`` then each worker process will call
1520 ``initializer(*initargs)`` when it starts.
1521
1522 .. method:: apply(func[, args[, kwds]])
1523
Benjamin Peterson37d2fe02008-10-24 22:28:58 +00001524 Call *func* with arguments *args* and keyword arguments *kwds*. It blocks
1525 till the result is ready.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001526
1527 .. method:: apply_async(func[, args[, kwds[, callback]]])
1528
1529 A variant of the :meth:`apply` method which returns a result object.
1530
1531 If *callback* is specified then it should be a callable which accepts a
1532 single argument. When the result becomes ready *callback* is applied to
1533 it (unless the call failed). *callback* should complete immediately since
1534 otherwise the thread which handles the results will get blocked.
1535
1536 .. method:: map(func, iterable[, chunksize])
1537
Georg Brandl92905032008-11-22 08:51:39 +00001538 A parallel equivalent of the :func:`map` builtin function, collecting the
1539 result in a list. It blocks till the whole result is ready.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001540
1541 This method chops the iterable into a number of chunks which it submits to
1542 the process pool as separate tasks. The (approximate) size of these
1543 chunks can be specified by setting *chunksize* to a positive integer.
1544
1545 .. method:: map_async(func, iterable[, chunksize[, callback]])
1546
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001547 A variant of the :meth:`map` method which returns a result object.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001548
1549 If *callback* is specified then it should be a callable which accepts a
1550 single argument. When the result becomes ready *callback* is applied to
1551 it (unless the call failed). *callback* should complete immediately since
1552 otherwise the thread which handles the results will get blocked.
1553
1554 .. method:: imap(func, iterable[, chunksize])
1555
Georg Brandl92905032008-11-22 08:51:39 +00001556 A lazier version of :meth:`map`.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001557
1558 The *chunksize* argument is the same as the one used by the :meth:`.map`
1559 method. For very long iterables using a large value for *chunksize* can
1560 make make the job complete **much** faster than using the default value of
1561 ``1``.
1562
1563 Also if *chunksize* is ``1`` then the :meth:`next` method of the iterator
1564 returned by the :meth:`imap` method has an optional *timeout* parameter:
1565 ``next(timeout)`` will raise :exc:`multiprocessing.TimeoutError` if the
1566 result cannot be returned within *timeout* seconds.
1567
1568 .. method:: imap_unordered(func, iterable[, chunksize])
1569
1570 The same as :meth:`imap` except that the ordering of the results from the
1571 returned iterator should be considered arbitrary. (Only when there is
1572 only one worker process is the order guaranteed to be "correct".)
1573
1574 .. method:: close()
1575
1576 Prevents any more tasks from being submitted to the pool. Once all the
1577 tasks have been completed the worker processes will exit.
1578
1579 .. method:: terminate()
1580
1581 Stops the worker processes immediately without completing outstanding
1582 work. When the pool object is garbage collected :meth:`terminate` will be
1583 called immediately.
1584
1585 .. method:: join()
1586
1587 Wait for the worker processes to exit. One must call :meth:`close` or
1588 :meth:`terminate` before using :meth:`join`.
1589
1590
1591.. class:: AsyncResult
1592
1593 The class of the result returned by :meth:`Pool.apply_async` and
1594 :meth:`Pool.map_async`.
1595
Georg Brandle3d70ae2008-11-22 08:54:21 +00001596 .. method:: get([timeout])
Benjamin Petersone711caf2008-06-11 16:44:04 +00001597
1598 Return the result when it arrives. If *timeout* is not ``None`` and the
1599 result does not arrive within *timeout* seconds then
1600 :exc:`multiprocessing.TimeoutError` is raised. If the remote call raised
1601 an exception then that exception will be reraised by :meth:`get`.
1602
1603 .. method:: wait([timeout])
1604
1605 Wait until the result is available or until *timeout* seconds pass.
1606
1607 .. method:: ready()
1608
1609 Return whether the call has completed.
1610
1611 .. method:: successful()
1612
1613 Return whether the call completed without raising an exception. Will
1614 raise :exc:`AssertionError` if the result is not ready.
1615
1616The following example demonstrates the use of a pool::
1617
1618 from multiprocessing import Pool
1619
1620 def f(x):
1621 return x*x
1622
1623 if __name__ == '__main__':
1624 pool = Pool(processes=4) # start 4 worker processes
1625
Georg Brandle3d70ae2008-11-22 08:54:21 +00001626 result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
Georg Brandl49702152008-09-29 06:43:45 +00001627 print(result.get(timeout=1)) # prints "100" unless your computer is *very* slow
Benjamin Petersone711caf2008-06-11 16:44:04 +00001628
Georg Brandl49702152008-09-29 06:43:45 +00001629 print(pool.map(f, range(10))) # prints "[0, 1, 4,..., 81]"
Benjamin Petersone711caf2008-06-11 16:44:04 +00001630
1631 it = pool.imap(f, range(10))
Georg Brandl49702152008-09-29 06:43:45 +00001632 print(next(it)) # prints "0"
1633 print(next(it)) # prints "1"
1634 print(it.next(timeout=1)) # prints "4" unless your computer is *very* slow
Benjamin Petersone711caf2008-06-11 16:44:04 +00001635
1636 import time
Georg Brandle3d70ae2008-11-22 08:54:21 +00001637 result = pool.apply_async(time.sleep, (10,))
Georg Brandl49702152008-09-29 06:43:45 +00001638 print(result.get(timeout=1)) # raises TimeoutError
Benjamin Petersone711caf2008-06-11 16:44:04 +00001639
1640
1641.. _multiprocessing-listeners-clients:
1642
1643Listeners and Clients
1644~~~~~~~~~~~~~~~~~~~~~
1645
1646.. module:: multiprocessing.connection
1647 :synopsis: API for dealing with sockets.
1648
1649Usually message passing between processes is done using queues or by using
1650:class:`Connection` objects returned by :func:`Pipe`.
1651
1652However, the :mod:`multiprocessing.connection` module allows some extra
1653flexibility. It basically gives a high level message oriented API for dealing
1654with sockets or Windows named pipes, and also has support for *digest
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001655authentication* using the :mod:`hmac` module.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001656
1657
1658.. function:: deliver_challenge(connection, authkey)
1659
1660 Send a randomly generated message to the other end of the connection and wait
1661 for a reply.
1662
1663 If the reply matches the digest of the message using *authkey* as the key
1664 then a welcome message is sent to the other end of the connection. Otherwise
1665 :exc:`AuthenticationError` is raised.
1666
1667.. function:: answerChallenge(connection, authkey)
1668
1669 Receive a message, calculate the digest of the message using *authkey* as the
1670 key, and then send the digest back.
1671
1672 If a welcome message is not received, then :exc:`AuthenticationError` is
1673 raised.
1674
1675.. function:: Client(address[, family[, authenticate[, authkey]]])
1676
1677 Attempt to set up a connection to the listener which is using address
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001678 *address*, returning a :class:`~multiprocessing.Connection`.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001679
1680 The type of the connection is determined by *family* argument, but this can
1681 generally be omitted since it can usually be inferred from the format of
1682 *address*. (See :ref:`multiprocessing-address-formats`)
1683
1684 If *authentication* is ``True`` or *authkey* is a string then digest
1685 authentication is used. The key used for authentication will be either
Benjamin Petersona786b022008-08-25 21:05:21 +00001686 *authkey* or ``current_process().authkey)`` if *authkey* is ``None``.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001687 If authentication fails then :exc:`AuthenticationError` is raised. See
1688 :ref:`multiprocessing-auth-keys`.
1689
1690.. class:: Listener([address[, family[, backlog[, authenticate[, authkey]]]]])
1691
1692 A wrapper for a bound socket or Windows named pipe which is 'listening' for
1693 connections.
1694
1695 *address* is the address to be used by the bound socket or named pipe of the
1696 listener object.
1697
1698 *family* is the type of socket (or named pipe) to use. This can be one of
1699 the strings ``'AF_INET'`` (for a TCP socket), ``'AF_UNIX'`` (for a Unix
1700 domain socket) or ``'AF_PIPE'`` (for a Windows named pipe). Of these only
1701 the first is guaranteed to be available. If *family* is ``None`` then the
1702 family is inferred from the format of *address*. If *address* is also
1703 ``None`` then a default is chosen. This default is the family which is
1704 assumed to be the fastest available. See
1705 :ref:`multiprocessing-address-formats`. Note that if *family* is
1706 ``'AF_UNIX'`` and address is ``None`` then the socket will be created in a
1707 private temporary directory created using :func:`tempfile.mkstemp`.
1708
1709 If the listener object uses a socket then *backlog* (1 by default) is passed
1710 to the :meth:`listen` method of the socket once it has been bound.
1711
1712 If *authenticate* is ``True`` (``False`` by default) or *authkey* is not
1713 ``None`` then digest authentication is used.
1714
1715 If *authkey* is a string then it will be used as the authentication key;
1716 otherwise it must be *None*.
1717
1718 If *authkey* is ``None`` and *authenticate* is ``True`` then
Benjamin Petersona786b022008-08-25 21:05:21 +00001719 ``current_process().authkey`` is used as the authentication key. If
Benjamin Petersone711caf2008-06-11 16:44:04 +00001720 *authkey* is ``None`` and *authentication* is ``False`` then no
1721 authentication is done. If authentication fails then
1722 :exc:`AuthenticationError` is raised. See :ref:`multiprocessing-auth-keys`.
1723
1724 .. method:: accept()
1725
1726 Accept a connection on the bound socket or named pipe of the listener
1727 object and return a :class:`Connection` object. If authentication is
1728 attempted and fails, then :exc:`AuthenticationError` is raised.
1729
1730 .. method:: close()
1731
1732 Close the bound socket or named pipe of the listener object. This is
1733 called automatically when the listener is garbage collected. However it
1734 is advisable to call it explicitly.
1735
1736 Listener objects have the following read-only properties:
1737
1738 .. attribute:: address
1739
1740 The address which is being used by the Listener object.
1741
1742 .. attribute:: last_accepted
1743
1744 The address from which the last accepted connection came. If this is
1745 unavailable then it is ``None``.
1746
1747
1748The module defines two exceptions:
1749
1750.. exception:: AuthenticationError
1751
1752 Exception raised when there is an authentication error.
1753
Benjamin Petersone711caf2008-06-11 16:44:04 +00001754
1755**Examples**
1756
1757The following server code creates a listener which uses ``'secret password'`` as
1758an authentication key. It then waits for a connection and sends some data to
1759the client::
1760
1761 from multiprocessing.connection import Listener
1762 from array import array
1763
1764 address = ('localhost', 6000) # family is deduced to be 'AF_INET'
1765 listener = Listener(address, authkey='secret password')
1766
1767 conn = listener.accept()
Georg Brandl49702152008-09-29 06:43:45 +00001768 print('connection accepted from', listener.last_accepted)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001769
1770 conn.send([2.25, None, 'junk', float])
1771
1772 conn.send_bytes('hello')
1773
1774 conn.send_bytes(array('i', [42, 1729]))
1775
1776 conn.close()
1777 listener.close()
1778
1779The following code connects to the server and receives some data from the
1780server::
1781
1782 from multiprocessing.connection import Client
1783 from array import array
1784
1785 address = ('localhost', 6000)
1786 conn = Client(address, authkey='secret password')
1787
Georg Brandl49702152008-09-29 06:43:45 +00001788 print(conn.recv()) # => [2.25, None, 'junk', float]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001789
Georg Brandl49702152008-09-29 06:43:45 +00001790 print(conn.recv_bytes()) # => 'hello'
Benjamin Petersone711caf2008-06-11 16:44:04 +00001791
1792 arr = array('i', [0, 0, 0, 0, 0])
Georg Brandl49702152008-09-29 06:43:45 +00001793 print(conn.recv_bytes_into(arr)) # => 8
1794 print(arr) # => array('i', [42, 1729, 0, 0, 0])
Benjamin Petersone711caf2008-06-11 16:44:04 +00001795
1796 conn.close()
1797
1798
1799.. _multiprocessing-address-formats:
1800
1801Address Formats
1802>>>>>>>>>>>>>>>
1803
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001804* An ``'AF_INET'`` address is a tuple of the form ``(hostname, port)`` where
Benjamin Petersone711caf2008-06-11 16:44:04 +00001805 *hostname* is a string and *port* is an integer.
1806
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001807* An ``'AF_UNIX'`` address is a string representing a filename on the
Benjamin Petersone711caf2008-06-11 16:44:04 +00001808 filesystem.
1809
1810* An ``'AF_PIPE'`` address is a string of the form
1811 ``r'\\\\.\\pipe\\PipeName'``. To use :func:`Client` to connect to a named
1812 pipe on a remote computer called ServerName* one should use an address of the
1813 form ``r'\\\\ServerName\\pipe\\PipeName'`` instead.
1814
1815Note that any string beginning with two backslashes is assumed by default to be
1816an ``'AF_PIPE'`` address rather than an ``'AF_UNIX'`` address.
1817
1818
1819.. _multiprocessing-auth-keys:
1820
1821Authentication keys
1822~~~~~~~~~~~~~~~~~~~
1823
1824When one uses :meth:`Connection.recv`, the data received is automatically
1825unpickled. Unfortunately unpickling data from an untrusted source is a security
1826risk. Therefore :class:`Listener` and :func:`Client` use the :mod:`hmac` module
1827to provide digest authentication.
1828
1829An authentication key is a string which can be thought of as a password: once a
1830connection is established both ends will demand proof that the other knows the
1831authentication key. (Demonstrating that both ends are using the same key does
1832**not** involve sending the key over the connection.)
1833
1834If authentication is requested but do authentication key is specified then the
Benjamin Petersona786b022008-08-25 21:05:21 +00001835return value of ``current_process().authkey`` is used (see
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001836:class:`~multiprocessing.Process`). This value will automatically inherited by
1837any :class:`~multiprocessing.Process` object that the current process creates.
1838This means that (by default) all processes of a multi-process program will share
1839a single authentication key which can be used when setting up connections
1840between the themselves.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001841
1842Suitable authentication keys can also be generated by using :func:`os.urandom`.
1843
1844
1845Logging
1846~~~~~~~
1847
1848Some support for logging is available. Note, however, that the :mod:`logging`
1849package does not use process shared locks so it is possible (depending on the
1850handler type) for messages from different processes to get mixed up.
1851
1852.. currentmodule:: multiprocessing
1853.. function:: get_logger()
1854
1855 Returns the logger used by :mod:`multiprocessing`. If necessary, a new one
1856 will be created.
1857
1858 When first created the logger has level :data:`logging.NOTSET` and has a
1859 handler which sends output to :data:`sys.stderr` using format
1860 ``'[%(levelname)s/%(processName)s] %(message)s'``. (The logger allows use of
1861 the non-standard ``'%(processName)s'`` format.) Message sent to this logger
Georg Brandl2ee470f2008-07-16 12:55:28 +00001862 will not by default propagate to the root logger.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001863
1864 Note that on Windows child processes will only inherit the level of the
1865 parent process's logger -- any other customization of the logger will not be
1866 inherited.
1867
1868Below is an example session with logging turned on::
1869
Benjamin Peterson206e3072008-10-19 14:07:49 +00001870 >>> import multiprocessing, logging
Benjamin Petersonf608c612008-11-16 18:33:53 +00001871 >>> logger = multiprocessing.get_logger()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001872 >>> logger.setLevel(logging.INFO)
1873 >>> logger.warning('doomed')
1874 [WARNING/MainProcess] doomed
Benjamin Peterson206e3072008-10-19 14:07:49 +00001875 >>> m = multiprocessing.Manager()
Benjamin Petersone711caf2008-06-11 16:44:04 +00001876 [INFO/SyncManager-1] child process calling self.run()
1877 [INFO/SyncManager-1] manager bound to '\\\\.\\pipe\\pyc-2776-0-lj0tfa'
1878 >>> del m
1879 [INFO/MainProcess] sending shutdown message to manager
1880 [INFO/SyncManager-1] manager exiting with exitcode 0
1881
1882
1883The :mod:`multiprocessing.dummy` module
1884~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1885
1886.. module:: multiprocessing.dummy
1887 :synopsis: Dumb wrapper around threading.
1888
1889:mod:`multiprocessing.dummy` replicates the API of :mod:`multiprocessing` but is
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001890no more than a wrapper around the :mod:`threading` module.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001891
1892
1893.. _multiprocessing-programming:
1894
1895Programming guidelines
1896----------------------
1897
1898There are certain guidelines and idioms which should be adhered to when using
1899:mod:`multiprocessing`.
1900
1901
1902All platforms
1903~~~~~~~~~~~~~
1904
1905Avoid shared state
1906
1907 As far as possible one should try to avoid shifting large amounts of data
1908 between processes.
1909
1910 It is probably best to stick to using queues or pipes for communication
1911 between processes rather than using the lower level synchronization
1912 primitives from the :mod:`threading` module.
1913
1914Picklability
1915
1916 Ensure that the arguments to the methods of proxies are picklable.
1917
1918Thread safety of proxies
1919
1920 Do not use a proxy object from more than one thread unless you protect it
1921 with a lock.
1922
1923 (There is never a problem with different processes using the *same* proxy.)
1924
1925Joining zombie processes
1926
1927 On Unix when a process finishes but has not been joined it becomes a zombie.
1928 There should never be very many because each time a new process starts (or
1929 :func:`active_children` is called) all completed processes which have not
1930 yet been joined will be joined. Also calling a finished process's
1931 :meth:`Process.is_alive` will join the process. Even so it is probably good
1932 practice to explicitly join all the processes that you start.
1933
1934Better to inherit than pickle/unpickle
1935
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001936 On Windows many types from :mod:`multiprocessing` need to be picklable so
Benjamin Petersone711caf2008-06-11 16:44:04 +00001937 that child processes can use them. However, one should generally avoid
1938 sending shared objects to other processes using pipes or queues. Instead
1939 you should arrange the program so that a process which need access to a
1940 shared resource created elsewhere can inherit it from an ancestor process.
1941
1942Avoid terminating processes
1943
1944 Using the :meth:`Process.terminate` method to stop a process is liable to
1945 cause any shared resources (such as locks, semaphores, pipes and queues)
1946 currently being used by the process to become broken or unavailable to other
1947 processes.
1948
1949 Therefore it is probably best to only consider using
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001950 :meth:`Process.terminate` on processes which never use any shared resources.
Benjamin Petersone711caf2008-06-11 16:44:04 +00001951
1952Joining processes that use queues
1953
1954 Bear in mind that a process that has put items in a queue will wait before
1955 terminating until all the buffered items are fed by the "feeder" thread to
1956 the underlying pipe. (The child process can call the
Benjamin Petersonae5360b2008-09-08 23:05:23 +00001957 :meth:`Queue.cancel_join_thread` method of the queue to avoid this behaviour.)
Benjamin Petersone711caf2008-06-11 16:44:04 +00001958
1959 This means that whenever you use a queue you need to make sure that all
1960 items which have been put on the queue will eventually be removed before the
1961 process is joined. Otherwise you cannot be sure that processes which have
1962 put items on the queue will terminate. Remember also that non-daemonic
1963 processes will be automatically be joined.
1964
1965 An example which will deadlock is the following::
1966
1967 from multiprocessing import Process, Queue
1968
1969 def f(q):
1970 q.put('X' * 1000000)
1971
1972 if __name__ == '__main__':
1973 queue = Queue()
1974 p = Process(target=f, args=(queue,))
1975 p.start()
1976 p.join() # this deadlocks
1977 obj = queue.get()
1978
1979 A fix here would be to swap the last two lines round (or simply remove the
1980 ``p.join()`` line).
1981
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00001982Explicitly pass resources to child processes
Benjamin Petersone711caf2008-06-11 16:44:04 +00001983
1984 On Unix a child process can make use of a shared resource created in a
1985 parent process using a global resource. However, it is better to pass the
1986 object as an argument to the constructor for the child process.
1987
1988 Apart from making the code (potentially) compatible with Windows this also
1989 ensures that as long as the child process is still alive the object will not
1990 be garbage collected in the parent process. This might be important if some
1991 resource is freed when the object is garbage collected in the parent
1992 process.
1993
1994 So for instance ::
1995
1996 from multiprocessing import Process, Lock
1997
1998 def f():
1999 ... do something using "lock" ...
2000
2001 if __name__ == '__main__':
2002 lock = Lock()
2003 for i in range(10):
2004 Process(target=f).start()
2005
2006 should be rewritten as ::
2007
2008 from multiprocessing import Process, Lock
2009
2010 def f(l):
2011 ... do something using "l" ...
2012
2013 if __name__ == '__main__':
2014 lock = Lock()
2015 for i in range(10):
2016 Process(target=f, args=(lock,)).start()
2017
2018
2019Windows
2020~~~~~~~
2021
2022Since Windows lacks :func:`os.fork` it has a few extra restrictions:
2023
2024More picklability
2025
2026 Ensure that all arguments to :meth:`Process.__init__` are picklable. This
2027 means, in particular, that bound or unbound methods cannot be used directly
2028 as the ``target`` argument on Windows --- just define a function and use
2029 that instead.
2030
2031 Also, if you subclass :class:`Process` then make sure that instances will be
2032 picklable when the :meth:`Process.start` method is called.
2033
2034Global variables
2035
2036 Bear in mind that if code run in a child process tries to access a global
2037 variable, then the value it sees (if any) may not be the same as the value
2038 in the parent process at the time that :meth:`Process.start` was called.
2039
2040 However, global variables which are just module level constants cause no
2041 problems.
2042
2043Safe importing of main module
2044
2045 Make sure that the main module can be safely imported by a new Python
2046 interpreter without causing unintended side effects (such a starting a new
2047 process).
2048
2049 For example, under Windows running the following module would fail with a
2050 :exc:`RuntimeError`::
2051
2052 from multiprocessing import Process
2053
2054 def foo():
Georg Brandl49702152008-09-29 06:43:45 +00002055 print('hello')
Benjamin Petersone711caf2008-06-11 16:44:04 +00002056
2057 p = Process(target=foo)
2058 p.start()
2059
2060 Instead one should protect the "entry point" of the program by using ``if
2061 __name__ == '__main__':`` as follows::
2062
2063 from multiprocessing import Process, freeze_support
2064
2065 def foo():
Georg Brandl49702152008-09-29 06:43:45 +00002066 print('hello')
Benjamin Petersone711caf2008-06-11 16:44:04 +00002067
2068 if __name__ == '__main__':
2069 freeze_support()
2070 p = Process(target=foo)
2071 p.start()
2072
Benjamin Peterson5289b2b2008-06-28 00:40:54 +00002073 (The ``freeze_support()`` line can be omitted if the program will be run
Benjamin Petersone711caf2008-06-11 16:44:04 +00002074 normally instead of frozen.)
2075
2076 This allows the newly spawned Python interpreter to safely import the module
2077 and then run the module's ``foo()`` function.
2078
2079 Similar restrictions apply if a pool or manager is created in the main
2080 module.
2081
2082
2083.. _multiprocessing-examples:
2084
2085Examples
2086--------
2087
2088Demonstration of how to create and use customized managers and proxies:
2089
2090.. literalinclude:: ../includes/mp_newtype.py
2091
2092
2093Using :class:`Pool`:
2094
2095.. literalinclude:: ../includes/mp_pool.py
2096
2097
2098Synchronization types like locks, conditions and queues:
2099
2100.. literalinclude:: ../includes/mp_synchronize.py
2101
2102
2103An showing how to use queues to feed tasks to a collection of worker process and
2104collect the results:
2105
2106.. literalinclude:: ../includes/mp_workers.py
2107
2108
2109An example of how a pool of worker processes can each run a
2110:class:`SimpleHTTPServer.HttpServer` instance while sharing a single listening
2111socket.
2112
2113.. literalinclude:: ../includes/mp_webserver.py
2114
2115
2116Some simple benchmarks comparing :mod:`multiprocessing` with :mod:`threading`:
2117
2118.. literalinclude:: ../includes/mp_benchmarks.py
2119
2120An example/demo of how to use the :class:`managers.SyncManager`, :class:`Process`
2121and others to build a system which can distribute processes and work via a
2122distributed queue to a "cluster" of machines on a network, accessible via SSH.
2123You will need to have private key authentication for all hosts configured for
2124this to work.
2125
Benjamin Peterson95a939c2008-06-14 02:23:29 +00002126.. literalinclude:: ../includes/mp_distributing.py