blob: 5432aa104b8a5a1f34eff9a36be8b728c5f2cee2 [file] [log] [blame]
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001:mod:`multiprocessing` --- Process-based "threading" interface
2==============================================================
3
4.. module:: multiprocessing
5 :synopsis: Process-based "threading" interface.
6
7.. versionadded:: 2.6
8
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00009
Benjamin Peterson190d56e2008-06-11 02:40:25 +000010Introduction
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +000011----------------------
Benjamin Peterson190d56e2008-06-11 02:40:25 +000012
Benjamin Peterson910c2ab2008-06-27 23:22:06 +000013:mod:`multiprocessing` is a package that supports spawning processes using an
14API similar to the :mod:`threading` module. The :mod:`multiprocessing` package
15offers both local and remote concurrency, effectively side-stepping the
16:term:`Global Interpreter Lock` by using subprocesses instead of threads. Due
17to this, the :mod:`multiprocessing` module allows the programmer to fully
18leverage multiple processors on a given machine. It runs on both Unix and
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +000019Windows.
Benjamin Peterson190d56e2008-06-11 02:40:25 +000020
Benjamin Peterson910c2ab2008-06-27 23:22:06 +000021
Benjamin Peterson190d56e2008-06-11 02:40:25 +000022The :class:`Process` class
23~~~~~~~~~~~~~~~~~~~~~~~~~~
24
25In :mod:`multiprocessing`, processes are spawned by creating a :class:`Process`
Benjamin Peterson910c2ab2008-06-27 23:22:06 +000026object and then calling its :meth:`~Process.start` method. :class:`Process`
Benjamin Peterson190d56e2008-06-11 02:40:25 +000027follows the API of :class:`threading.Thread`. A trivial example of a
28multiprocess program is ::
29
30 from multiprocessing import Process
31
32 def f(name):
33 print 'hello', name
34
35 if __name__ == '__main__':
36 p = Process(target=f, args=('bob',))
37 p.start()
38 p.join()
39
40Here the function ``f`` is run in a child process.
41
42For an explanation of why (on Windows) the ``if __name__ == '__main__'`` part is
43necessary, see :ref:`multiprocessing-programming`.
44
45
46
47Exchanging objects between processes
48~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50:mod:`multiprocessing` supports two types of communication channel between
51processes:
52
53**Queues**
54
55 The :class:`Queue` class is a near clone of :class:`Queue.Queue`. For
56 example::
57
58 from multiprocessing import Process, Queue
59
60 def f(q):
61 q.put([42, None, 'hello'])
62
63 if __name__ == '__main__':
64 q = Queue()
65 p = Process(target=f, args=(q,))
66 p.start()
67 print q.get() # prints "[42, None, 'hello']"
68 p.join()
69
70 Queues are thread and process safe.
71
72**Pipes**
73
74 The :func:`Pipe` function returns a pair of connection objects connected by a
75 pipe which by default is duplex (two-way). For example::
76
77 from multiprocessing import Process, Pipe
78
79 def f(conn):
80 conn.send([42, None, 'hello'])
81 conn.close()
82
83 if __name__ == '__main__':
84 parent_conn, child_conn = Pipe()
85 p = Process(target=f, args=(child_conn,))
86 p.start()
87 print parent_conn.recv() # prints "[42, None, 'hello']"
88 p.join()
89
90 The two connection objects returned by :func:`Pipe` represent the two ends of
Benjamin Peterson910c2ab2008-06-27 23:22:06 +000091 the pipe. Each connection object has :meth:`~Connection.send` and
92 :meth:`~Connection.recv` methods (among others). Note that data in a pipe
93 may become corrupted if two processes (or threads) try to read from or write
94 to the *same* end of the pipe at the same time. Of course there is no risk
95 of corruption from processes using different ends of the pipe at the same
96 time.
Benjamin Peterson190d56e2008-06-11 02:40:25 +000097
98
99Synchronization between processes
100~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101
102:mod:`multiprocessing` contains equivalents of all the synchronization
103primitives from :mod:`threading`. For instance one can use a lock to ensure
104that only one process prints to standard output at a time::
105
106 from multiprocessing import Process, Lock
107
108 def f(l, i):
109 l.acquire()
110 print 'hello world', i
111 l.release()
112
113 if __name__ == '__main__':
114 lock = Lock()
115
116 for num in range(10):
117 Process(target=f, args=(lock, num)).start()
118
119Without using the lock output from the different processes is liable to get all
120mixed up.
121
122
123Sharing state between processes
124~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
126As mentioned above, when doing concurrent programming it is usually best to
127avoid using shared state as far as possible. This is particularly true when
128using multiple processes.
129
130However, if you really do need to use some shared data then
131:mod:`multiprocessing` provides a couple of ways of doing so.
132
133**Shared memory**
134
135 Data can be stored in a shared memory map using :class:`Value` or
136 :class:`Array`. For example, the following code ::
137
138 from multiprocessing import Process, Value, Array
139
140 def f(n, a):
141 n.value = 3.1415927
142 for i in range(len(a)):
143 a[i] = -a[i]
144
145 if __name__ == '__main__':
146 num = Value('d', 0.0)
147 arr = Array('i', range(10))
148
149 p = Process(target=f, args=(num, arr))
150 p.start()
151 p.join()
152
153 print num.value
154 print arr[:]
155
156 will print ::
157
158 3.1415927
159 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
160
161 The ``'d'`` and ``'i'`` arguments used when creating ``num`` and ``arr`` are
162 typecodes of the kind used by the :mod:`array` module: ``'d'`` indicates a
Benjamin Peterson90f36732008-07-12 20:16:19 +0000163 double precision float and ``'i'`` indicates a signed integer. These shared
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000164 objects will be process and thread safe.
165
166 For more flexibility in using shared memory one can use the
167 :mod:`multiprocessing.sharedctypes` module which supports the creation of
168 arbitrary ctypes objects allocated from shared memory.
169
170**Server process**
171
172 A manager object returned by :func:`Manager` controls a server process which
Andrew M. Kuchlingded01d12008-07-14 00:35:32 +0000173 holds Python objects and allows other processes to manipulate them using
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000174 proxies.
175
176 A manager returned by :func:`Manager` will support types :class:`list`,
177 :class:`dict`, :class:`Namespace`, :class:`Lock`, :class:`RLock`,
178 :class:`Semaphore`, :class:`BoundedSemaphore`, :class:`Condition`,
179 :class:`Event`, :class:`Queue`, :class:`Value` and :class:`Array`. For
180 example, ::
181
182 from multiprocessing import Process, Manager
183
184 def f(d, l):
185 d[1] = '1'
186 d['2'] = 2
187 d[0.25] = None
188 l.reverse()
189
190 if __name__ == '__main__':
191 manager = Manager()
192
193 d = manager.dict()
194 l = manager.list(range(10))
195
196 p = Process(target=f, args=(d, l))
197 p.start()
198 p.join()
199
200 print d
201 print l
202
203 will print ::
204
205 {0.25: None, 1: '1', '2': 2}
206 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
207
208 Server process managers are more flexible than using shared memory objects
209 because they can be made to support arbitrary object types. Also, a single
210 manager can be shared by processes on different computers over a network.
211 They are, however, slower than using shared memory.
212
213
214Using a pool of workers
215~~~~~~~~~~~~~~~~~~~~~~~
216
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000217The :class:`~multiprocessing.pool.Pool` class represents a pool of worker
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000218processes. It has methods which allows tasks to be offloaded to the worker
219processes in a few different ways.
220
221For example::
222
223 from multiprocessing import Pool
224
225 def f(x):
226 return x*x
227
228 if __name__ == '__main__':
229 pool = Pool(processes=4) # start 4 worker processes
230 result = pool.applyAsync(f, [10]) # evaluate "f(10)" asynchronously
231 print result.get(timeout=1) # prints "100" unless your computer is *very* slow
232 print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
233
234
235Reference
236---------
237
238The :mod:`multiprocessing` package mostly replicates the API of the
239:mod:`threading` module.
240
241
242:class:`Process` and exceptions
243~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244
245.. class:: Process([group[, target[, name[, args[, kwargs]]]]])
246
247 Process objects represent activity that is run in a separate process. The
248 :class:`Process` class has equivalents of all the methods of
249 :class:`threading.Thread`.
250
251 The constructor should always be called with keyword arguments. *group*
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000252 should always be ``None``; it exists solely for compatibility with
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000253 :class:`~threading.Thread`. *target* is the callable object to be invoked by
254 the :meth:`run()` method. It defaults to ``None``, meaning nothing is
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000255 called. *name* is the process name. By default, a unique name is constructed
256 of the form 'Process-N\ :sub:`1`:N\ :sub:`2`:...:N\ :sub:`k`' where N\
257 :sub:`1`,N\ :sub:`2`,...,N\ :sub:`k` is a sequence of integers whose length
258 is determined by the *generation* of the process. *args* is the argument
259 tuple for the target invocation. *kwargs* is a dictionary of keyword
260 arguments for the target invocation. By default, no arguments are passed to
261 *target*.
262
263 If a subclass overrides the constructor, it must make sure it invokes the
264 base class constructor (:meth:`Process.__init__`) before doing anything else
265 to the process.
266
267 .. method:: run()
268
269 Method representing the process's activity.
270
271 You may override this method in a subclass. The standard :meth:`run`
272 method invokes the callable object passed to the object's constructor as
273 the target argument, if any, with sequential and keyword arguments taken
274 from the *args* and *kwargs* arguments, respectively.
275
276 .. method:: start()
277
278 Start the process's activity.
279
280 This must be called at most once per process object. It arranges for the
281 object's :meth:`run` method to be invoked in a separate process.
282
283 .. method:: join([timeout])
284
285 Block the calling thread until the process whose :meth:`join` method is
286 called terminates or until the optional timeout occurs.
287
288 If *timeout* is ``None`` then there is no timeout.
289
290 A process can be joined many times.
291
292 A process cannot join itself because this would cause a deadlock. It is
293 an error to attempt to join a process before it has been started.
294
295 .. method:: get_name()
296
297 Return the process's name.
298
299 .. method:: set_name(name)
300
301 Set the process's name.
302
303 The name is a string used for identification purposes only. It has no
304 semantics. Multiple processes may be given the same name. The initial
305 name is set by the constructor.
306
307 .. method:: is_alive()
308
309 Return whether the process is alive.
310
311 Roughly, a process object is alive from the moment the :meth:`start`
312 method returns until the child process terminates.
313
314 .. method:: is_daemon()
315
316 Return the process's daemon flag.
317
318 .. method:: set_daemon(daemonic)
319
320 Set the process's daemon flag to the Boolean value *daemonic*. This must
321 be called before :meth:`start` is called.
322
323 The initial value is inherited from the creating process.
324
325 When a process exits, it attempts to terminate all of its daemonic child
326 processes.
327
328 Note that a daemonic process is not allowed to create child processes.
329 Otherwise a daemonic process would leave its children orphaned if it gets
330 terminated when its parent process exits.
331
332 In addition process objects also support the following methods:
333
334 .. method:: get_pid()
335
336 Return the process ID. Before the process is spawned, this will be
337 ``None``.
338
339 .. method:: get_exit_code()
340
341 Return the child's exit code. This will be ``None`` if the process has
342 not yet terminated. A negative value *-N* indicates that the child was
343 terminated by signal *N*.
344
345 .. method:: get_auth_key()
346
347 Return the process's authentication key (a byte string).
348
349 When :mod:`multiprocessing` is initialized the main process is assigned a
350 random string using :func:`os.random`.
351
352 When a :class:`Process` object is created, it will inherit the
353 authentication key of its parent process, although this may be changed
354 using :meth:`set_auth_key` below.
355
356 See :ref:`multiprocessing-auth-keys`.
357
358 .. method:: set_auth_key(authkey)
359
360 Set the process's authentication key which must be a byte string.
361
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000362 .. method:: terminate()
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000363
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000364 Terminate the process. On Unix this is done using the ``SIGTERM`` signal;
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000365 on Windows :cfunc:`TerminateProcess` is used. Note that exit handlers and
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000366 finally clauses, etc., will not be executed.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000367
368 Note that descendant processes of the process will *not* be terminated --
369 they will simply become orphaned.
370
371 .. warning::
372
373 If this method is used when the associated process is using a pipe or
374 queue then the pipe or queue is liable to become corrupted and may
375 become unusable by other process. Similarly, if the process has
376 acquired a lock or semaphore etc. then terminating it is liable to
377 cause other processes to deadlock.
378
379 Note that the :meth:`start`, :meth:`join`, :meth:`is_alive` and
380 :meth:`get_exit_code` methods should only be called by the process that
381 created the process object.
382
383 Example usage of some of the methods of :class:`Process`::
384
385 >>> import processing, time, signal
386 >>> p = processing.Process(target=time.sleep, args=(1000,))
387 >>> print p, p.is_alive()
388 <Process(Process-1, initial)> False
389 >>> p.start()
390 >>> print p, p.is_alive()
391 <Process(Process-1, started)> True
392 >>> p.terminate()
393 >>> print p, p.is_alive()
394 <Process(Process-1, stopped[SIGTERM])> False
395 >>> p.get_exit_code() == -signal.SIGTERM
396 True
397
398
399.. exception:: BufferTooShort
400
401 Exception raised by :meth:`Connection.recv_bytes_into()` when the supplied
402 buffer object is too small for the message read.
403
404 If ``e`` is an instance of :exc:`BufferTooShort` then ``e.args[0]`` will give
405 the message as a byte string.
406
407
408Pipes and Queues
409~~~~~~~~~~~~~~~~
410
411When using multiple processes, one generally uses message passing for
412communication between processes and avoids having to use any synchronization
413primitives like locks.
414
415For passing messages one can use :func:`Pipe` (for a connection between two
416processes) or a queue (which allows multiple producers and consumers).
417
418The :class:`Queue` and :class:`JoinableQueue` types are multi-producer,
419multi-consumer FIFO queues modelled on the :class:`Queue.Queue` class in the
420standard library. They differ in that :class:`Queue` lacks the
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000421:meth:`~Queue.Queue.task_done` and :meth:`~Queue.Queue.join` methods introduced
422into Python 2.5's :class:`Queue.Queue` class.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000423
424If you use :class:`JoinableQueue` then you **must** call
425:meth:`JoinableQueue.task_done` for each task removed from the queue or else the
426semaphore used to count the number of unfinished tasks may eventually overflow
427raising an exception.
428
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000429Note that one can also create a shared queue by using a manager object -- see
430:ref:`multiprocessing-managers`.
431
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000432.. note::
433
434 :mod:`multiprocessing` uses the usual :exc:`Queue.Empty` and
435 :exc:`Queue.Full` exceptions to signal a timeout. They are not available in
436 the :mod:`multiprocessing` namespace so you need to import them from
437 :mod:`Queue`.
438
439
440.. warning::
441
442 If a process is killed using :meth:`Process.terminate` or :func:`os.kill`
443 while it is trying to use a :class:`Queue`, then the data in the queue is
444 likely to become corrupted. This may cause any other processes to get an
445 exception when it tries to use the queue later on.
446
447.. warning::
448
449 As mentioned above, if a child process has put items on a queue (and it has
450 not used :meth:`JoinableQueue.cancel_join_thread`), then that process will
451 not terminate until all buffered items have been flushed to the pipe.
452
453 This means that if you try joining that process you may get a deadlock unless
454 you are sure that all items which have been put on the queue have been
455 consumed. Similarly, if the child process is non-daemonic then the parent
Andrew M. Kuchlingded01d12008-07-14 00:35:32 +0000456 process may hang on exit when it tries to join all its non-daemonic children.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000457
458 Note that a queue created using a manager does not have this issue. See
459 :ref:`multiprocessing-programming`.
460
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000461For an example of the usage of queues for interprocess communication see
462:ref:`multiprocessing-examples`.
463
464
465.. function:: Pipe([duplex])
466
467 Returns a pair ``(conn1, conn2)`` of :class:`Connection` objects representing
468 the ends of a pipe.
469
470 If *duplex* is ``True`` (the default) then the pipe is bidirectional. If
471 *duplex* is ``False`` then the pipe is unidirectional: ``conn1`` can only be
472 used for receiving messages and ``conn2`` can only be used for sending
473 messages.
474
475
476.. class:: Queue([maxsize])
477
478 Returns a process shared queue implemented using a pipe and a few
479 locks/semaphores. When a process first puts an item on the queue a feeder
480 thread is started which transfers objects from a buffer into the pipe.
481
482 The usual :exc:`Queue.Empty` and :exc:`Queue.Full` exceptions from the
483 standard library's :mod:`Queue` module are raised to signal timeouts.
484
485 :class:`Queue` implements all the methods of :class:`Queue.Queue` except for
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000486 :meth:`~Queue.Queue.task_done` and :meth:`~Queue.Queue.join`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000487
488 .. method:: qsize()
489
490 Return the approximate size of the queue. Because of
491 multithreading/multiprocessing semantics, this number is not reliable.
492
493 Note that this may raise :exc:`NotImplementedError` on Unix platforms like
494 MacOS X where ``sem_getvalue()`` is not implemented.
495
496 .. method:: empty()
497
498 Return ``True`` if the queue is empty, ``False`` otherwise. Because of
499 multithreading/multiprocessing semantics, this is not reliable.
500
501 .. method:: full()
502
503 Return ``True`` if the queue is full, ``False`` otherwise. Because of
504 multithreading/multiprocessing semantics, this is not reliable.
505
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000506 .. method:: put(item[, block[, timeout]])
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000507
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +0000508 Put item into the queue. If the optional argument *block* is ``True``
509 (the default) and *timeout* is ``None`` (the default), block if necessary until
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000510 a free slot is available. If *timeout* is a positive number, it blocks at
511 most *timeout* seconds and raises the :exc:`Queue.Full` exception if no
512 free slot was available within that time. Otherwise (*block* is
513 ``False``), put an item on the queue if a free slot is immediately
514 available, else raise the :exc:`Queue.Full` exception (*timeout* is
515 ignored in that case).
516
517 .. method:: put_nowait(item)
518
519 Equivalent to ``put(item, False)``.
520
521 .. method:: get([block[, timeout]])
522
523 Remove and return an item from the queue. If optional args *block* is
524 ``True`` (the default) and *timeout* is ``None`` (the default), block if
525 necessary until an item is available. If *timeout* is a positive number,
526 it blocks at most *timeout* seconds and raises the :exc:`Queue.Empty`
527 exception if no item was available within that time. Otherwise (block is
528 ``False``), return an item if one is immediately available, else raise the
529 :exc:`Queue.Empty` exception (*timeout* is ignored in that case).
530
531 .. method:: get_nowait()
532 get_no_wait()
533
534 Equivalent to ``get(False)``.
535
536 :class:`multiprocessing.Queue` has a few additional methods not found in
Andrew M. Kuchlingded01d12008-07-14 00:35:32 +0000537 :class:`Queue.Queue`. These methods are usually unnecessary for most
538 code:
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000539
540 .. method:: close()
541
542 Indicate that no more data will be put on this queue by the current
543 process. The background thread will quit once it has flushed all buffered
544 data to the pipe. This is called automatically when the queue is garbage
545 collected.
546
547 .. method:: join_thread()
548
549 Join the background thread. This can only be used after :meth:`close` has
550 been called. It blocks until the background thread exits, ensuring that
551 all data in the buffer has been flushed to the pipe.
552
553 By default if a process is not the creator of the queue then on exit it
554 will attempt to join the queue's background thread. The process can call
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000555 :meth:`cancel_join_thread` to make :meth:`join_thread` do nothing.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000556
557 .. method:: cancel_join_thread()
558
559 Prevent :meth:`join_thread` from blocking. In particular, this prevents
560 the background thread from being joined automatically when the process
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000561 exits -- see :meth:`join_thread`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000562
563
564.. class:: JoinableQueue([maxsize])
565
566 :class:`JoinableQueue`, a :class:`Queue` subclass, is a queue which
567 additionally has :meth:`task_done` and :meth:`join` methods.
568
569 .. method:: task_done()
570
571 Indicate that a formerly enqueued task is complete. Used by queue consumer
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000572 threads. For each :meth:`~Queue.get` used to fetch a task, a subsequent
573 call to :meth:`task_done` tells the queue that the processing on the task
574 is complete.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000575
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000576 If a :meth:`~Queue.join` is currently blocking, it will resume when all
577 items have been processed (meaning that a :meth:`task_done` call was
578 received for every item that had been :meth:`~Queue.put` into the queue).
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000579
580 Raises a :exc:`ValueError` if called more times than there were items
581 placed in the queue.
582
583
584 .. method:: join()
585
586 Block until all items in the queue have been gotten and processed.
587
588 The count of unfinished tasks goes up whenever an item is added to the
589 queue. The count goes down whenever a consumer thread calls
590 :meth:`task_done` to indicate that the item was retrieved and all work on
591 it is complete. When the count of unfinished tasks drops to zero,
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000592 :meth:`~Queue.join` unblocks.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000593
594
595Miscellaneous
596~~~~~~~~~~~~~
597
598.. function:: active_children()
599
600 Return list of all live children of the current process.
601
602 Calling this has the side affect of "joining" any processes which have
603 already finished.
604
605.. function:: cpu_count()
606
607 Return the number of CPUs in the system. May raise
608 :exc:`NotImplementedError`.
609
610.. function:: current_process()
611
612 Return the :class:`Process` object corresponding to the current process.
613
614 An analogue of :func:`threading.current_thread`.
615
616.. function:: freeze_support()
617
618 Add support for when a program which uses :mod:`multiprocessing` has been
619 frozen to produce a Windows executable. (Has been tested with **py2exe**,
620 **PyInstaller** and **cx_Freeze**.)
621
622 One needs to call this function straight after the ``if __name__ ==
623 '__main__'`` line of the main module. For example::
624
625 from multiprocessing import Process, freeze_support
626
627 def f():
628 print 'hello world!'
629
630 if __name__ == '__main__':
631 freeze_support()
632 Process(target=f).start()
633
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000634 If the ``freeze_support()`` line is missed out then trying to run the frozen
635 executable will raise :exc:`RuntimeError`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000636
637 If the module is being run normally by the Python interpreter then
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000638 :func:`freeze_support` has no effect.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000639
640.. function:: set_executable()
641
642 Sets the path of the python interpreter to use when starting a child process.
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000643 (By default :data:`sys.executable` is used). Embedders will probably need to
644 do some thing like ::
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000645
646 setExecutable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
647
648 before they can create child processes. (Windows only)
649
650
651.. note::
652
653 :mod:`multiprocessing` contains no analogues of
654 :func:`threading.active_count`, :func:`threading.enumerate`,
655 :func:`threading.settrace`, :func:`threading.setprofile`,
656 :class:`threading.Timer`, or :class:`threading.local`.
657
658
659Connection Objects
660~~~~~~~~~~~~~~~~~~
661
662Connection objects allow the sending and receiving of picklable objects or
663strings. They can be thought of as message oriented connected sockets.
664
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000665Connection objects usually created using :func:`Pipe` -- see also
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000666:ref:`multiprocessing-listeners-clients`.
667
668.. class:: Connection
669
670 .. method:: send(obj)
671
672 Send an object to the other end of the connection which should be read
673 using :meth:`recv`.
674
675 The object must be picklable.
676
677 .. method:: recv()
678
679 Return an object sent from the other end of the connection using
680 :meth:`send`. Raises :exc:`EOFError` if there is nothing left to receive
681 and the other end was closed.
682
683 .. method:: fileno()
684
685 Returns the file descriptor or handle used by the connection.
686
687 .. method:: close()
688
689 Close the connection.
690
691 This is called automatically when the connection is garbage collected.
692
693 .. method:: poll([timeout])
694
695 Return whether there is any data available to be read.
696
697 If *timeout* is not specified then it will return immediately. If
698 *timeout* is a number then this specifies the maximum time in seconds to
699 block. If *timeout* is ``None`` then an infinite timeout is used.
700
701 .. method:: send_bytes(buffer[, offset[, size]])
702
703 Send byte data from an object supporting the buffer interface as a
704 complete message.
705
706 If *offset* is given then data is read from that position in *buffer*. If
707 *size* is given then that many bytes will be read from buffer.
708
709 .. method:: recv_bytes([maxlength])
710
711 Return a complete message of byte data sent from the other end of the
712 connection as a string. Raises :exc:`EOFError` if there is nothing left
713 to receive and the other end has closed.
714
715 If *maxlength* is specified and the message is longer than *maxlength*
716 then :exc:`IOError` is raised and the connection will no longer be
717 readable.
718
719 .. method:: recv_bytes_into(buffer[, offset])
720
721 Read into *buffer* a complete message of byte data sent from the other end
722 of the connection and return the number of bytes in the message. Raises
723 :exc:`EOFError` if there is nothing left to receive and the other end was
724 closed.
725
726 *buffer* must be an object satisfying the writable buffer interface. If
727 *offset* is given then the message will be written into the buffer from
728 *that position. Offset must be a non-negative integer less than the
729 *length of *buffer* (in bytes).
730
731 If the buffer is too short then a :exc:`BufferTooShort` exception is
732 raised and the complete message is available as ``e.args[0]`` where ``e``
733 is the exception instance.
734
735
736For example:
737
738 >>> from multiprocessing import Pipe
739 >>> a, b = Pipe()
740 >>> a.send([1, 'hello', None])
741 >>> b.recv()
742 [1, 'hello', None]
743 >>> b.send_bytes('thank you')
744 >>> a.recv_bytes()
745 'thank you'
746 >>> import array
747 >>> arr1 = array.array('i', range(5))
748 >>> arr2 = array.array('i', [0] * 10)
749 >>> a.send_bytes(arr1)
750 >>> count = b.recv_bytes_into(arr2)
751 >>> assert count == len(arr1) * arr1.itemsize
752 >>> arr2
753 array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])
754
755
756.. warning::
757
758 The :meth:`Connection.recv` method automatically unpickles the data it
759 receives, which can be a security risk unless you can trust the process
760 which sent the message.
761
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000762 Therefore, unless the connection object was produced using :func:`Pipe` you
763 should only use the :meth:`~Connection.recv` and :meth:`~Connection.send`
764 methods after performing some sort of authentication. See
765 :ref:`multiprocessing-auth-keys`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000766
767.. warning::
768
769 If a process is killed while it is trying to read or write to a pipe then
770 the data in the pipe is likely to become corrupted, because it may become
771 impossible to be sure where the message boundaries lie.
772
773
774Synchronization primitives
775~~~~~~~~~~~~~~~~~~~~~~~~~~
776
777Generally synchronization primitives are not as necessary in a multiprocess
Andrew M. Kuchling8ea605c2008-07-14 01:18:16 +0000778program as they are in a multithreaded program. See the documentation for
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000779:mod:`threading` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000780
781Note that one can also create synchronization primitives by using a manager
782object -- see :ref:`multiprocessing-managers`.
783
784.. class:: BoundedSemaphore([value])
785
786 A bounded semaphore object: a clone of :class:`threading.BoundedSemaphore`.
787
Benjamin Peterson90f36732008-07-12 20:16:19 +0000788 (On Mac OSX this is indistinguishable from :class:`Semaphore` because
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000789 ``sem_getvalue()`` is not implemented on that platform).
790
791.. class:: Condition([lock])
792
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000793 A condition variable: a clone of :class:`threading.Condition`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000794
795 If *lock* is specified then it should be a :class:`Lock` or :class:`RLock`
796 object from :mod:`multiprocessing`.
797
798.. class:: Event()
799
800 A clone of :class:`threading.Event`.
801
802.. class:: Lock()
803
804 A non-recursive lock object: a clone of :class:`threading.Lock`.
805
806.. class:: RLock()
807
808 A recursive lock object: a clone of :class:`threading.RLock`.
809
810.. class:: Semaphore([value])
811
812 A bounded semaphore object: a clone of :class:`threading.Semaphore`.
813
814.. note::
815
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000816 The :meth:`acquire` method of :class:`BoundedSemaphore`, :class:`Lock`,
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000817 :class:`RLock` and :class:`Semaphore` has a timeout parameter not supported
818 by the equivalents in :mod:`threading`. The signature is
819 ``acquire(block=True, timeout=None)`` with keyword parameters being
820 acceptable. If *block* is ``True`` and *timeout* is not ``None`` then it
821 specifies a timeout in seconds. If *block* is ``False`` then *timeout* is
822 ignored.
823
824.. note::
825
826 If the SIGINT signal generated by Ctrl-C arrives while the main thread is
827 blocked by a call to :meth:`BoundedSemaphore.acquire`, :meth:`Lock.acquire`,
828 :meth:`RLock.acquire`, :meth:`Semaphore.acquire`, :meth:`Condition.acquire`
829 or :meth:`Condition.wait` then the call will be immediately interrupted and
830 :exc:`KeyboardInterrupt` will be raised.
831
832 This differs from the behaviour of :mod:`threading` where SIGINT will be
833 ignored while the equivalent blocking calls are in progress.
834
835
836Shared :mod:`ctypes` Objects
837~~~~~~~~~~~~~~~~~~~~~~~~~~~~
838
839It is possible to create shared objects using shared memory which can be
840inherited by child processes.
841
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000842.. function:: Value(typecode_or_type[, *args, lock]])
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000843
844 Return a :mod:`ctypes` object allocated from shared memory. By default the
845 return value is actually a synchronized wrapper for the object.
846
847 *typecode_or_type* determines the type of the returned object: it is either a
848 ctypes type or a one character typecode of the kind used by the :mod:`array`
849 module. *\*args* is passed on to the constructor for the type.
850
851 If *lock* is ``True`` (the default) then a new lock object is created to
852 synchronize access to the value. If *lock* is a :class:`Lock` or
853 :class:`RLock` object then that will be used to synchronize access to the
854 value. If *lock* is ``False`` then access to the returned object will not be
855 automatically protected by a lock, so it will not necessarily be
856 "process-safe".
857
858 Note that *lock* is a keyword-only argument.
859
860.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True)
861
862 Return a ctypes array allocated from shared memory. By default the return
863 value is actually a synchronized wrapper for the array.
864
865 *typecode_or_type* determines the type of the elements of the returned array:
866 it is either a ctypes type or a one character typecode of the kind used by
867 the :mod:`array` module. If *size_or_initializer* is an integer, then it
868 determines the length of the array, and the array will be initially zeroed.
869 Otherwise, *size_or_initializer* is a sequence which is used to initialize
870 the array and whose length determines the length of the array.
871
872 If *lock* is ``True`` (the default) then a new lock object is created to
873 synchronize access to the value. If *lock* is a :class:`Lock` or
874 :class:`RLock` object then that will be used to synchronize access to the
875 value. If *lock* is ``False`` then access to the returned object will not be
876 automatically protected by a lock, so it will not necessarily be
877 "process-safe".
878
879 Note that *lock* is a keyword only argument.
880
881 Note that an array of :data:`ctypes.c_char` has *value* and *rawvalue*
882 attributes which allow one to use it to store and retrieve strings.
883
884
885The :mod:`multiprocessing.sharedctypes` module
886>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
887
888.. module:: multiprocessing.sharedctypes
889 :synopsis: Allocate ctypes objects from shared memory.
890
891The :mod:`multiprocessing.sharedctypes` module provides functions for allocating
892:mod:`ctypes` objects from shared memory which can be inherited by child
893processes.
894
895.. note::
896
Benjamin Peterson90f36732008-07-12 20:16:19 +0000897 Although it is possible to store a pointer in shared memory remember that
898 this will refer to a location in the address space of a specific process.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000899 However, the pointer is quite likely to be invalid in the context of a second
900 process and trying to dereference the pointer from the second process may
901 cause a crash.
902
903.. function:: RawArray(typecode_or_type, size_or_initializer)
904
905 Return a ctypes array allocated from shared memory.
906
907 *typecode_or_type* determines the type of the elements of the returned array:
908 it is either a ctypes type or a one character typecode of the kind used by
909 the :mod:`array` module. If *size_or_initializer* is an integer then it
910 determines the length of the array, and the array will be initially zeroed.
911 Otherwise *size_or_initializer* is a sequence which is used to initialize the
912 array and whose length determines the length of the array.
913
914 Note that setting and getting an element is potentially non-atomic -- use
915 :func:`Array` instead to make sure that access is automatically synchronized
916 using a lock.
917
918.. function:: RawValue(typecode_or_type, *args)
919
920 Return a ctypes object allocated from shared memory.
921
922 *typecode_or_type* determines the type of the returned object: it is either a
923 ctypes type or a one character typecode of the kind used by the :mod:`array`
924 module. */*args* is passed on to the constructor for the type.
925
926 Note that setting and getting the value is potentially non-atomic -- use
927 :func:`Value` instead to make sure that access is automatically synchronized
928 using a lock.
929
930 Note that an array of :data:`ctypes.c_char` has ``value`` and ``rawvalue``
931 attributes which allow one to use it to store and retrieve strings -- see
932 documentation for :mod:`ctypes`.
933
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000934.. function:: Array(typecode_or_type, size_or_initializer[, *args[, lock]])
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000935
936 The same as :func:`RawArray` except that depending on the value of *lock* a
937 process-safe synchronization wrapper may be returned instead of a raw ctypes
938 array.
939
940 If *lock* is ``True`` (the default) then a new lock object is created to
941 synchronize access to the value. If *lock* is a :class:`Lock` or
942 :class:`RLock` object then that will be used to synchronize access to the
943 value. If *lock* is ``False`` then access to the returned object will not be
944 automatically protected by a lock, so it will not necessarily be
945 "process-safe".
946
947 Note that *lock* is a keyword-only argument.
948
949.. function:: Value(typecode_or_type, *args[, lock])
950
951 The same as :func:`RawValue` except that depending on the value of *lock* a
952 process-safe synchronization wrapper may be returned instead of a raw ctypes
953 object.
954
955 If *lock* is ``True`` (the default) then a new lock object is created to
956 synchronize access to the value. If *lock* is a :class:`Lock` or
957 :class:`RLock` object then that will be used to synchronize access to the
958 value. If *lock* is ``False`` then access to the returned object will not be
959 automatically protected by a lock, so it will not necessarily be
960 "process-safe".
961
962 Note that *lock* is a keyword-only argument.
963
964.. function:: copy(obj)
965
966 Return a ctypes object allocated from shared memory which is a copy of the
967 ctypes object *obj*.
968
969.. function:: synchronized(obj[, lock])
970
971 Return a process-safe wrapper object for a ctypes object which uses *lock* to
972 synchronize access. If *lock* is ``None`` (the default) then a
973 :class:`multiprocessing.RLock` object is created automatically.
974
975 A synchronized wrapper will have two methods in addition to those of the
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000976 object it wraps: :meth:`get_obj` returns the wrapped object and
977 :meth:`get_lock` returns the lock object used for synchronization.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000978
979 Note that accessing the ctypes object through the wrapper can be a lot slower
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000980 than accessing the raw ctypes object.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000981
982
983The table below compares the syntax for creating shared ctypes objects from
984shared memory with the normal ctypes syntax. (In the table ``MyStruct`` is some
985subclass of :class:`ctypes.Structure`.)
986
987==================== ========================== ===========================
988ctypes sharedctypes using type sharedctypes using typecode
989==================== ========================== ===========================
990c_double(2.4) RawValue(c_double, 2.4) RawValue('d', 2.4)
991MyStruct(4, 6) RawValue(MyStruct, 4, 6)
992(c_short * 7)() RawArray(c_short, 7) RawArray('h', 7)
993(c_int * 3)(9, 2, 8) RawArray(c_int, (9, 2, 8)) RawArray('i', (9, 2, 8))
994==================== ========================== ===========================
995
996
997Below is an example where a number of ctypes objects are modified by a child
998process::
999
1000 from multiprocessing import Process, Lock
1001 from multiprocessing.sharedctypes import Value, Array
1002 from ctypes import Structure, c_double
1003
1004 class Point(Structure):
1005 _fields_ = [('x', c_double), ('y', c_double)]
1006
1007 def modify(n, x, s, A):
1008 n.value **= 2
1009 x.value **= 2
1010 s.value = s.value.upper()
1011 for a in A:
1012 a.x **= 2
1013 a.y **= 2
1014
1015 if __name__ == '__main__':
1016 lock = Lock()
1017
1018 n = Value('i', 7)
1019 x = Value(ctypes.c_double, 1.0/3.0, lock=False)
1020 s = Array('c', 'hello world', lock=lock)
1021 A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)
1022
1023 p = Process(target=modify, args=(n, x, s, A))
1024 p.start()
1025 p.join()
1026
1027 print n.value
1028 print x.value
1029 print s.value
1030 print [(a.x, a.y) for a in A]
1031
1032
1033.. highlightlang:: none
1034
1035The results printed are ::
1036
1037 49
1038 0.1111111111111111
1039 HELLO WORLD
1040 [(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]
1041
1042.. highlightlang:: python
1043
1044
1045.. _multiprocessing-managers:
1046
1047Managers
1048~~~~~~~~
1049
1050Managers provide a way to create data which can be shared between different
1051processes. A manager object controls a server process which manages *shared
1052objects*. Other processes can access the shared objects by using proxies.
1053
1054.. function:: multiprocessing.Manager()
1055
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001056 Returns a started :class:`~multiprocessing.managers.SyncManager` object which
1057 can be used for sharing objects between processes. The returned manager
1058 object corresponds to a spawned child process and has methods which will
1059 create shared objects and return corresponding proxies.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001060
1061.. module:: multiprocessing.managers
1062 :synopsis: Share data between process with shared objects.
1063
1064Manager processes will be shutdown as soon as they are garbage collected or
1065their parent process exits. The manager classes are defined in the
1066:mod:`multiprocessing.managers` module:
1067
1068.. class:: BaseManager([address[, authkey]])
1069
1070 Create a BaseManager object.
1071
1072 Once created one should call :meth:`start` or :meth:`serve_forever` to ensure
1073 that the manager object refers to a started manager process.
1074
1075 *address* is the address on which the manager process listens for new
1076 connections. If *address* is ``None`` then an arbitrary one is chosen.
1077
1078 *authkey* is the authentication key which will be used to check the validity
1079 of incoming connections to the server process. If *authkey* is ``None`` then
1080 ``current_process().get_auth_key()``. Otherwise *authkey* is used and it
1081 must be a string.
1082
1083 .. method:: start()
1084
1085 Start a subprocess to start the manager.
1086
Andrew M. Kuchlinga2478d92008-07-14 00:40:55 +00001087 .. method:: serve_forever()
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001088
1089 Run the server in the current process.
1090
1091 .. method:: from_address(address, authkey)
1092
1093 A class method which creates a manager object referring to a pre-existing
1094 server process which is using the given address and authentication key.
1095
1096 .. method:: shutdown()
1097
1098 Stop the process used by the manager. This is only available if
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001099 :meth:`start` has been used to start the server process.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001100
1101 This can be called multiple times.
1102
1103 .. method:: register(typeid[, callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]])
1104
1105 A classmethod which can be used for registering a type or callable with
1106 the manager class.
1107
1108 *typeid* is a "type identifier" which is used to identify a particular
1109 type of shared object. This must be a string.
1110
1111 *callable* is a callable used for creating objects for this type
1112 identifier. If a manager instance will be created using the
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001113 :meth:`from_address` classmethod or if the *create_method* argument is
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001114 ``False`` then this can be left as ``None``.
1115
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001116 *proxytype* is a subclass of :class:`BaseProxy` which is used to create
1117 proxies for shared objects with this *typeid*. If ``None`` then a proxy
1118 class is created automatically.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001119
1120 *exposed* is used to specify a sequence of method names which proxies for
1121 this typeid should be allowed to access using
1122 :meth:`BaseProxy._callMethod`. (If *exposed* is ``None`` then
1123 :attr:`proxytype._exposed_` is used instead if it exists.) In the case
1124 where no exposed list is specified, all "public methods" of the shared
1125 object will be accessible. (Here a "public method" means any attribute
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001126 which has a :meth:`__call__` method and whose name does not begin with
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001127 ``'_'``.)
1128
1129 *method_to_typeid* is a mapping used to specify the return type of those
1130 exposed methods which should return a proxy. It maps method names to
1131 typeid strings. (If *method_to_typeid* is ``None`` then
1132 :attr:`proxytype._method_to_typeid_` is used instead if it exists.) If a
1133 method's name is not a key of this mapping or if the mapping is ``None``
1134 then the object returned by the method will be copied by value.
1135
1136 *create_method* determines whether a method should be created with name
1137 *typeid* which can be used to tell the server process to create a new
1138 shared object and return a proxy for it. By default it is ``True``.
1139
1140 :class:`BaseManager` instances also have one read-only property:
1141
1142 .. attribute:: address
1143
1144 The address used by the manager.
1145
1146
1147.. class:: SyncManager
1148
1149 A subclass of :class:`BaseManager` which can be used for the synchronization
1150 of processes. Objects of this type are returned by
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001151 :func:`multiprocessing.Manager`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001152
1153 It also supports creation of shared lists and dictionaries.
1154
1155 .. method:: BoundedSemaphore([value])
1156
1157 Create a shared :class:`threading.BoundedSemaphore` object and return a
1158 proxy for it.
1159
1160 .. method:: Condition([lock])
1161
1162 Create a shared :class:`threading.Condition` object and return a proxy for
1163 it.
1164
1165 If *lock* is supplied then it should be a proxy for a
1166 :class:`threading.Lock` or :class:`threading.RLock` object.
1167
1168 .. method:: Event()
1169
1170 Create a shared :class:`threading.Event` object and return a proxy for it.
1171
1172 .. method:: Lock()
1173
1174 Create a shared :class:`threading.Lock` object and return a proxy for it.
1175
1176 .. method:: Namespace()
1177
1178 Create a shared :class:`Namespace` object and return a proxy for it.
1179
1180 .. method:: Queue([maxsize])
1181
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001182 Create a shared :class:`Queue.Queue` object and return a proxy for it.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001183
1184 .. method:: RLock()
1185
1186 Create a shared :class:`threading.RLock` object and return a proxy for it.
1187
1188 .. method:: Semaphore([value])
1189
1190 Create a shared :class:`threading.Semaphore` object and return a proxy for
1191 it.
1192
1193 .. method:: Array(typecode, sequence)
1194
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001195 Create an array and return a proxy for it.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001196
1197 .. method:: Value(typecode, value)
1198
1199 Create an object with a writable ``value`` attribute and return a proxy
1200 for it.
1201
1202 .. method:: dict()
1203 dict(mapping)
1204 dict(sequence)
1205
1206 Create a shared ``dict`` object and return a proxy for it.
1207
1208 .. method:: list()
1209 list(sequence)
1210
1211 Create a shared ``list`` object and return a proxy for it.
1212
1213
1214Namespace objects
1215>>>>>>>>>>>>>>>>>
1216
1217A namespace object has no public methods, but does have writable attributes.
1218Its representation shows the values of its attributes.
1219
1220However, when using a proxy for a namespace object, an attribute beginning with
1221``'_'`` will be an attribute of the proxy and not an attribute of the referent::
1222
1223 >>> manager = multiprocessing.Manager()
1224 >>> Global = manager.Namespace()
1225 >>> Global.x = 10
1226 >>> Global.y = 'hello'
1227 >>> Global._z = 12.3 # this is an attribute of the proxy
1228 >>> print Global
1229 Namespace(x=10, y='hello')
1230
1231
1232Customized managers
1233>>>>>>>>>>>>>>>>>>>
1234
1235To create one's own manager, one creates a subclass of :class:`BaseManager` and
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001236use the :meth:`~BaseManager.resgister` classmethod to register new types or
1237callables with the manager class. For example::
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001238
1239 from multiprocessing.managers import BaseManager
1240
1241 class MathsClass(object):
1242 def add(self, x, y):
1243 return x + y
1244 def mul(self, x, y):
1245 return x * y
1246
1247 class MyManager(BaseManager):
1248 pass
1249
1250 MyManager.register('Maths', MathsClass)
1251
1252 if __name__ == '__main__':
1253 manager = MyManager()
1254 manager.start()
1255 maths = manager.Maths()
1256 print maths.add(4, 3) # prints 7
1257 print maths.mul(7, 8) # prints 56
1258
1259
1260Using a remote manager
1261>>>>>>>>>>>>>>>>>>>>>>
1262
1263It is possible to run a manager server on one machine and have clients use it
1264from other machines (assuming that the firewalls involved allow it).
1265
1266Running the following commands creates a server for a single shared queue which
1267remote clients can access::
1268
1269 >>> from multiprocessing.managers import BaseManager
1270 >>> import Queue
1271 >>> queue = Queue.Queue()
1272 >>> class QueueManager(BaseManager): pass
1273 ...
1274 >>> QueueManager.register('getQueue', callable=lambda:queue)
1275 >>> m = QueueManager(address=('', 50000), authkey='abracadabra')
1276 >>> m.serveForever()
1277
1278One client can access the server as follows::
1279
1280 >>> from multiprocessing.managers import BaseManager
1281 >>> class QueueManager(BaseManager): pass
1282 ...
1283 >>> QueueManager.register('getQueue')
1284 >>> m = QueueManager.from_address(address=('foo.bar.org', 50000),
1285 >>> authkey='abracadabra')
1286 >>> queue = m.getQueue()
1287 >>> queue.put('hello')
1288
1289Another client can also use it::
1290
1291 >>> from multiprocessing.managers import BaseManager
1292 >>> class QueueManager(BaseManager): pass
1293 ...
1294 >>> QueueManager.register('getQueue')
1295 >>> m = QueueManager.from_address(address=('foo.bar.org', 50000), authkey='abracadabra')
1296 >>> queue = m.getQueue()
1297 >>> queue.get()
1298 'hello'
1299
1300
1301Proxy Objects
1302~~~~~~~~~~~~~
1303
1304A proxy is an object which *refers* to a shared object which lives (presumably)
1305in a different process. The shared object is said to be the *referent* of the
1306proxy. Multiple proxy objects may have the same referent.
1307
1308A proxy object has methods which invoke corresponding methods of its referent
1309(although not every method of the referent will necessarily be available through
1310the proxy). A proxy can usually be used in most of the same ways that its
1311referent can::
1312
1313 >>> from multiprocessing import Manager
1314 >>> manager = Manager()
1315 >>> l = manager.list([i*i for i in range(10)])
1316 >>> print l
1317 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
1318 >>> print repr(l)
1319 <ListProxy object, typeid 'list' at 0xb799974c>
1320 >>> l[4]
1321 16
1322 >>> l[2:5]
1323 [4, 9, 16]
1324
1325Notice that applying :func:`str` to a proxy will return the representation of
1326the referent, whereas applying :func:`repr` will return the representation of
1327the proxy.
1328
1329An important feature of proxy objects is that they are picklable so they can be
1330passed between processes. Note, however, that if a proxy is sent to the
1331corresponding manager's process then unpickling it will produce the referent
1332itself. This means, for example, that one shared object can contain a second::
1333
1334 >>> a = manager.list()
1335 >>> b = manager.list()
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001336 >>> a.append(b) # referent of a now contains referent of b
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001337 >>> print a, b
1338 [[]] []
1339 >>> b.append('hello')
1340 >>> print a, b
1341 [['hello']] ['hello']
1342
1343.. note::
1344
1345 The proxy types in :mod:`multiprocessing` do nothing to support comparisons
1346 by value. So, for instance, ::
1347
1348 manager.list([1,2,3]) == [1,2,3]
1349
1350 will return ``False``. One should just use a copy of the referent instead
1351 when making comparisons.
1352
1353.. class:: BaseProxy
1354
1355 Proxy objects are instances of subclasses of :class:`BaseProxy`.
1356
1357 .. method:: _call_method(methodname[, args[, kwds]])
1358
1359 Call and return the result of a method of the proxy's referent.
1360
1361 If ``proxy`` is a proxy whose referent is ``obj`` then the expression ::
1362
1363 proxy._call_method(methodname, args, kwds)
1364
1365 will evaluate the expression ::
1366
1367 getattr(obj, methodname)(*args, **kwds)
1368
1369 in the manager's process.
1370
1371 The returned value will be a copy of the result of the call or a proxy to
1372 a new shared object -- see documentation for the *method_to_typeid*
1373 argument of :meth:`BaseManager.register`.
1374
1375 If an exception is raised by the call, then then is re-raised by
1376 :meth:`_call_method`. If some other exception is raised in the manager's
1377 process then this is converted into a :exc:`RemoteError` exception and is
1378 raised by :meth:`_call_method`.
1379
1380 Note in particular that an exception will be raised if *methodname* has
1381 not been *exposed*
1382
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001383 An example of the usage of :meth:`_call_method`::
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001384
1385 >>> l = manager.list(range(10))
1386 >>> l._call_method('__len__')
1387 10
1388 >>> l._call_method('__getslice__', (2, 7)) # equiv to `l[2:7]`
1389 [2, 3, 4, 5, 6]
1390 >>> l._call_method('__getitem__', (20,)) # equiv to `l[20]`
1391 Traceback (most recent call last):
1392 ...
1393 IndexError: list index out of range
1394
1395 .. method:: _get_value()
1396
1397 Return a copy of the referent.
1398
1399 If the referent is unpicklable then this will raise an exception.
1400
1401 .. method:: __repr__
1402
1403 Return a representation of the proxy object.
1404
1405 .. method:: __str__
1406
1407 Return the representation of the referent.
1408
1409
1410Cleanup
1411>>>>>>>
1412
1413A proxy object uses a weakref callback so that when it gets garbage collected it
1414deregisters itself from the manager which owns its referent.
1415
1416A shared object gets deleted from the manager process when there are no longer
1417any proxies referring to it.
1418
1419
1420Process Pools
1421~~~~~~~~~~~~~
1422
1423.. module:: multiprocessing.pool
1424 :synopsis: Create pools of processes.
1425
1426One can create a pool of processes which will carry out tasks submitted to it
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001427with the :class:`Pool` class.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001428
1429.. class:: multiprocessing.Pool([processes[, initializer[, initargs]]])
1430
1431 A process pool object which controls a pool of worker processes to which jobs
1432 can be submitted. It supports asynchronous results with timeouts and
1433 callbacks and has a parallel map implementation.
1434
1435 *processes* is the number of worker processes to use. If *processes* is
1436 ``None`` then the number returned by :func:`cpu_count` is used. If
1437 *initializer* is not ``None`` then each worker process will call
1438 ``initializer(*initargs)`` when it starts.
1439
1440 .. method:: apply(func[, args[, kwds]])
1441
1442 Equivalent of the :func:`apply` builtin function. It blocks till the
1443 result is ready.
1444
1445 .. method:: apply_async(func[, args[, kwds[, callback]]])
1446
1447 A variant of the :meth:`apply` method which returns a result object.
1448
1449 If *callback* is specified then it should be a callable which accepts a
1450 single argument. When the result becomes ready *callback* is applied to
1451 it (unless the call failed). *callback* should complete immediately since
1452 otherwise the thread which handles the results will get blocked.
1453
1454 .. method:: map(func, iterable[, chunksize])
1455
1456 A parallel equivalent of the :func:`map` builtin function. It blocks till
1457 the result is ready.
1458
1459 This method chops the iterable into a number of chunks which it submits to
1460 the process pool as separate tasks. The (approximate) size of these
1461 chunks can be specified by setting *chunksize* to a positive integer.
1462
1463 .. method:: map_async(func, iterable[, chunksize[, callback]])
1464
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001465 A variant of the :meth:`map` method which returns a result object.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001466
1467 If *callback* is specified then it should be a callable which accepts a
1468 single argument. When the result becomes ready *callback* is applied to
1469 it (unless the call failed). *callback* should complete immediately since
1470 otherwise the thread which handles the results will get blocked.
1471
1472 .. method:: imap(func, iterable[, chunksize])
1473
1474 An equivalent of :func:`itertools.imap`.
1475
1476 The *chunksize* argument is the same as the one used by the :meth:`.map`
1477 method. For very long iterables using a large value for *chunksize* can
1478 make make the job complete **much** faster than using the default value of
1479 ``1``.
1480
1481 Also if *chunksize* is ``1`` then the :meth:`next` method of the iterator
1482 returned by the :meth:`imap` method has an optional *timeout* parameter:
1483 ``next(timeout)`` will raise :exc:`multiprocessing.TimeoutError` if the
1484 result cannot be returned within *timeout* seconds.
1485
1486 .. method:: imap_unordered(func, iterable[, chunksize])
1487
1488 The same as :meth:`imap` except that the ordering of the results from the
1489 returned iterator should be considered arbitrary. (Only when there is
1490 only one worker process is the order guaranteed to be "correct".)
1491
1492 .. method:: close()
1493
1494 Prevents any more tasks from being submitted to the pool. Once all the
1495 tasks have been completed the worker processes will exit.
1496
1497 .. method:: terminate()
1498
1499 Stops the worker processes immediately without completing outstanding
1500 work. When the pool object is garbage collected :meth:`terminate` will be
1501 called immediately.
1502
1503 .. method:: join()
1504
1505 Wait for the worker processes to exit. One must call :meth:`close` or
1506 :meth:`terminate` before using :meth:`join`.
1507
1508
1509.. class:: AsyncResult
1510
1511 The class of the result returned by :meth:`Pool.apply_async` and
1512 :meth:`Pool.map_async`.
1513
1514 .. method:: get([timeout)
1515
1516 Return the result when it arrives. If *timeout* is not ``None`` and the
1517 result does not arrive within *timeout* seconds then
1518 :exc:`multiprocessing.TimeoutError` is raised. If the remote call raised
1519 an exception then that exception will be reraised by :meth:`get`.
1520
1521 .. method:: wait([timeout])
1522
1523 Wait until the result is available or until *timeout* seconds pass.
1524
1525 .. method:: ready()
1526
1527 Return whether the call has completed.
1528
1529 .. method:: successful()
1530
1531 Return whether the call completed without raising an exception. Will
1532 raise :exc:`AssertionError` if the result is not ready.
1533
1534The following example demonstrates the use of a pool::
1535
1536 from multiprocessing import Pool
1537
1538 def f(x):
1539 return x*x
1540
1541 if __name__ == '__main__':
1542 pool = Pool(processes=4) # start 4 worker processes
1543
1544 result = pool.applyAsync(f, (10,)) # evaluate "f(10)" asynchronously
1545 print result.get(timeout=1) # prints "100" unless your computer is *very* slow
1546
1547 print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
1548
1549 it = pool.imap(f, range(10))
1550 print it.next() # prints "0"
1551 print it.next() # prints "1"
1552 print it.next(timeout=1) # prints "4" unless your computer is *very* slow
1553
1554 import time
1555 result = pool.applyAsync(time.sleep, (10,))
1556 print result.get(timeout=1) # raises TimeoutError
1557
1558
1559.. _multiprocessing-listeners-clients:
1560
1561Listeners and Clients
1562~~~~~~~~~~~~~~~~~~~~~
1563
1564.. module:: multiprocessing.connection
1565 :synopsis: API for dealing with sockets.
1566
1567Usually message passing between processes is done using queues or by using
1568:class:`Connection` objects returned by :func:`Pipe`.
1569
1570However, the :mod:`multiprocessing.connection` module allows some extra
1571flexibility. It basically gives a high level message oriented API for dealing
1572with sockets or Windows named pipes, and also has support for *digest
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001573authentication* using the :mod:`hmac` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001574
1575
1576.. function:: deliver_challenge(connection, authkey)
1577
1578 Send a randomly generated message to the other end of the connection and wait
1579 for a reply.
1580
1581 If the reply matches the digest of the message using *authkey* as the key
1582 then a welcome message is sent to the other end of the connection. Otherwise
1583 :exc:`AuthenticationError` is raised.
1584
1585.. function:: answerChallenge(connection, authkey)
1586
1587 Receive a message, calculate the digest of the message using *authkey* as the
1588 key, and then send the digest back.
1589
1590 If a welcome message is not received, then :exc:`AuthenticationError` is
1591 raised.
1592
1593.. function:: Client(address[, family[, authenticate[, authkey]]])
1594
1595 Attempt to set up a connection to the listener which is using address
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001596 *address*, returning a :class:`~multiprocessing.Connection`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001597
1598 The type of the connection is determined by *family* argument, but this can
1599 generally be omitted since it can usually be inferred from the format of
1600 *address*. (See :ref:`multiprocessing-address-formats`)
1601
1602 If *authentication* is ``True`` or *authkey* is a string then digest
1603 authentication is used. The key used for authentication will be either
1604 *authkey* or ``current_process().get_auth_key()`` if *authkey* is ``None``.
1605 If authentication fails then :exc:`AuthenticationError` is raised. See
1606 :ref:`multiprocessing-auth-keys`.
1607
1608.. class:: Listener([address[, family[, backlog[, authenticate[, authkey]]]]])
1609
1610 A wrapper for a bound socket or Windows named pipe which is 'listening' for
1611 connections.
1612
1613 *address* is the address to be used by the bound socket or named pipe of the
1614 listener object.
1615
1616 *family* is the type of socket (or named pipe) to use. This can be one of
1617 the strings ``'AF_INET'`` (for a TCP socket), ``'AF_UNIX'`` (for a Unix
1618 domain socket) or ``'AF_PIPE'`` (for a Windows named pipe). Of these only
1619 the first is guaranteed to be available. If *family* is ``None`` then the
1620 family is inferred from the format of *address*. If *address* is also
1621 ``None`` then a default is chosen. This default is the family which is
1622 assumed to be the fastest available. See
1623 :ref:`multiprocessing-address-formats`. Note that if *family* is
1624 ``'AF_UNIX'`` and address is ``None`` then the socket will be created in a
1625 private temporary directory created using :func:`tempfile.mkstemp`.
1626
1627 If the listener object uses a socket then *backlog* (1 by default) is passed
1628 to the :meth:`listen` method of the socket once it has been bound.
1629
1630 If *authenticate* is ``True`` (``False`` by default) or *authkey* is not
1631 ``None`` then digest authentication is used.
1632
1633 If *authkey* is a string then it will be used as the authentication key;
1634 otherwise it must be *None*.
1635
1636 If *authkey* is ``None`` and *authenticate* is ``True`` then
1637 ``current_process().get_auth_key()`` is used as the authentication key. If
1638 *authkey* is ``None`` and *authentication* is ``False`` then no
1639 authentication is done. If authentication fails then
1640 :exc:`AuthenticationError` is raised. See :ref:`multiprocessing-auth-keys`.
1641
1642 .. method:: accept()
1643
1644 Accept a connection on the bound socket or named pipe of the listener
1645 object and return a :class:`Connection` object. If authentication is
1646 attempted and fails, then :exc:`AuthenticationError` is raised.
1647
1648 .. method:: close()
1649
1650 Close the bound socket or named pipe of the listener object. This is
1651 called automatically when the listener is garbage collected. However it
1652 is advisable to call it explicitly.
1653
1654 Listener objects have the following read-only properties:
1655
1656 .. attribute:: address
1657
1658 The address which is being used by the Listener object.
1659
1660 .. attribute:: last_accepted
1661
1662 The address from which the last accepted connection came. If this is
1663 unavailable then it is ``None``.
1664
1665
1666The module defines two exceptions:
1667
1668.. exception:: AuthenticationError
1669
1670 Exception raised when there is an authentication error.
1671
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001672
1673**Examples**
1674
1675The following server code creates a listener which uses ``'secret password'`` as
1676an authentication key. It then waits for a connection and sends some data to
1677the client::
1678
1679 from multiprocessing.connection import Listener
1680 from array import array
1681
1682 address = ('localhost', 6000) # family is deduced to be 'AF_INET'
1683 listener = Listener(address, authkey='secret password')
1684
1685 conn = listener.accept()
1686 print 'connection accepted from', listener.last_accepted
1687
1688 conn.send([2.25, None, 'junk', float])
1689
1690 conn.send_bytes('hello')
1691
1692 conn.send_bytes(array('i', [42, 1729]))
1693
1694 conn.close()
1695 listener.close()
1696
1697The following code connects to the server and receives some data from the
1698server::
1699
1700 from multiprocessing.connection import Client
1701 from array import array
1702
1703 address = ('localhost', 6000)
1704 conn = Client(address, authkey='secret password')
1705
1706 print conn.recv() # => [2.25, None, 'junk', float]
1707
1708 print conn.recv_bytes() # => 'hello'
1709
1710 arr = array('i', [0, 0, 0, 0, 0])
1711 print conn.recv_bytes_into(arr) # => 8
1712 print arr # => array('i', [42, 1729, 0, 0, 0])
1713
1714 conn.close()
1715
1716
1717.. _multiprocessing-address-formats:
1718
1719Address Formats
1720>>>>>>>>>>>>>>>
1721
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001722* An ``'AF_INET'`` address is a tuple of the form ``(hostname, port)`` where
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001723 *hostname* is a string and *port* is an integer.
1724
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001725* An ``'AF_UNIX'`` address is a string representing a filename on the
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001726 filesystem.
1727
1728* An ``'AF_PIPE'`` address is a string of the form
1729 ``r'\\\\.\\pipe\\PipeName'``. To use :func:`Client` to connect to a named
1730 pipe on a remote computer called ServerName* one should use an address of the
1731 form ``r'\\\\ServerName\\pipe\\PipeName'`` instead.
1732
1733Note that any string beginning with two backslashes is assumed by default to be
1734an ``'AF_PIPE'`` address rather than an ``'AF_UNIX'`` address.
1735
1736
1737.. _multiprocessing-auth-keys:
1738
1739Authentication keys
1740~~~~~~~~~~~~~~~~~~~
1741
1742When one uses :meth:`Connection.recv`, the data received is automatically
1743unpickled. Unfortunately unpickling data from an untrusted source is a security
1744risk. Therefore :class:`Listener` and :func:`Client` use the :mod:`hmac` module
1745to provide digest authentication.
1746
1747An authentication key is a string which can be thought of as a password: once a
1748connection is established both ends will demand proof that the other knows the
1749authentication key. (Demonstrating that both ends are using the same key does
1750**not** involve sending the key over the connection.)
1751
1752If authentication is requested but do authentication key is specified then the
1753return value of ``current_process().get_auth_key`` is used (see
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001754:class:`~multiprocessing.Process`). This value will automatically inherited by
1755any :class:`~multiprocessing.Process` object that the current process creates.
1756This means that (by default) all processes of a multi-process program will share
1757a single authentication key which can be used when setting up connections
1758between the themselves.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001759
1760Suitable authentication keys can also be generated by using :func:`os.urandom`.
1761
1762
1763Logging
1764~~~~~~~
1765
1766Some support for logging is available. Note, however, that the :mod:`logging`
1767package does not use process shared locks so it is possible (depending on the
1768handler type) for messages from different processes to get mixed up.
1769
1770.. currentmodule:: multiprocessing
1771.. function:: get_logger()
1772
1773 Returns the logger used by :mod:`multiprocessing`. If necessary, a new one
1774 will be created.
1775
1776 When first created the logger has level :data:`logging.NOTSET` and has a
1777 handler which sends output to :data:`sys.stderr` using format
1778 ``'[%(levelname)s/%(processName)s] %(message)s'``. (The logger allows use of
1779 the non-standard ``'%(processName)s'`` format.) Message sent to this logger
Benjamin Peterson90f36732008-07-12 20:16:19 +00001780 will not by default propagate to the root logger.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001781
1782 Note that on Windows child processes will only inherit the level of the
1783 parent process's logger -- any other customization of the logger will not be
1784 inherited.
1785
1786Below is an example session with logging turned on::
1787
1788 >>> import processing, logging
1789 >>> logger = processing.getLogger()
1790 >>> logger.setLevel(logging.INFO)
1791 >>> logger.warning('doomed')
1792 [WARNING/MainProcess] doomed
1793 >>> m = processing.Manager()
1794 [INFO/SyncManager-1] child process calling self.run()
1795 [INFO/SyncManager-1] manager bound to '\\\\.\\pipe\\pyc-2776-0-lj0tfa'
1796 >>> del m
1797 [INFO/MainProcess] sending shutdown message to manager
1798 [INFO/SyncManager-1] manager exiting with exitcode 0
1799
1800
1801The :mod:`multiprocessing.dummy` module
1802~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1803
1804.. module:: multiprocessing.dummy
1805 :synopsis: Dumb wrapper around threading.
1806
1807:mod:`multiprocessing.dummy` replicates the API of :mod:`multiprocessing` but is
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001808no more than a wrapper around the :mod:`threading` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001809
1810
1811.. _multiprocessing-programming:
1812
1813Programming guidelines
1814----------------------
1815
1816There are certain guidelines and idioms which should be adhered to when using
1817:mod:`multiprocessing`.
1818
1819
1820All platforms
1821~~~~~~~~~~~~~
1822
1823Avoid shared state
1824
1825 As far as possible one should try to avoid shifting large amounts of data
1826 between processes.
1827
1828 It is probably best to stick to using queues or pipes for communication
1829 between processes rather than using the lower level synchronization
1830 primitives from the :mod:`threading` module.
1831
1832Picklability
1833
1834 Ensure that the arguments to the methods of proxies are picklable.
1835
1836Thread safety of proxies
1837
1838 Do not use a proxy object from more than one thread unless you protect it
1839 with a lock.
1840
1841 (There is never a problem with different processes using the *same* proxy.)
1842
1843Joining zombie processes
1844
1845 On Unix when a process finishes but has not been joined it becomes a zombie.
1846 There should never be very many because each time a new process starts (or
1847 :func:`active_children` is called) all completed processes which have not
1848 yet been joined will be joined. Also calling a finished process's
1849 :meth:`Process.is_alive` will join the process. Even so it is probably good
1850 practice to explicitly join all the processes that you start.
1851
1852Better to inherit than pickle/unpickle
1853
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001854 On Windows many types from :mod:`multiprocessing` need to be picklable so
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001855 that child processes can use them. However, one should generally avoid
1856 sending shared objects to other processes using pipes or queues. Instead
1857 you should arrange the program so that a process which need access to a
1858 shared resource created elsewhere can inherit it from an ancestor process.
1859
1860Avoid terminating processes
1861
1862 Using the :meth:`Process.terminate` method to stop a process is liable to
1863 cause any shared resources (such as locks, semaphores, pipes and queues)
1864 currently being used by the process to become broken or unavailable to other
1865 processes.
1866
1867 Therefore it is probably best to only consider using
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001868 :meth:`Process.terminate` on processes which never use any shared resources.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001869
1870Joining processes that use queues
1871
1872 Bear in mind that a process that has put items in a queue will wait before
1873 terminating until all the buffered items are fed by the "feeder" thread to
1874 the underlying pipe. (The child process can call the
1875 :meth:`Queue.cancel_join` method of the queue to avoid this behaviour.)
1876
1877 This means that whenever you use a queue you need to make sure that all
1878 items which have been put on the queue will eventually be removed before the
1879 process is joined. Otherwise you cannot be sure that processes which have
1880 put items on the queue will terminate. Remember also that non-daemonic
1881 processes will be automatically be joined.
1882
1883 An example which will deadlock is the following::
1884
1885 from multiprocessing import Process, Queue
1886
1887 def f(q):
1888 q.put('X' * 1000000)
1889
1890 if __name__ == '__main__':
1891 queue = Queue()
1892 p = Process(target=f, args=(queue,))
1893 p.start()
1894 p.join() # this deadlocks
1895 obj = queue.get()
1896
1897 A fix here would be to swap the last two lines round (or simply remove the
1898 ``p.join()`` line).
1899
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001900Explicitly pass resources to child processes
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001901
1902 On Unix a child process can make use of a shared resource created in a
1903 parent process using a global resource. However, it is better to pass the
1904 object as an argument to the constructor for the child process.
1905
1906 Apart from making the code (potentially) compatible with Windows this also
1907 ensures that as long as the child process is still alive the object will not
1908 be garbage collected in the parent process. This might be important if some
1909 resource is freed when the object is garbage collected in the parent
1910 process.
1911
1912 So for instance ::
1913
1914 from multiprocessing import Process, Lock
1915
1916 def f():
1917 ... do something using "lock" ...
1918
1919 if __name__ == '__main__':
1920 lock = Lock()
1921 for i in range(10):
1922 Process(target=f).start()
1923
1924 should be rewritten as ::
1925
1926 from multiprocessing import Process, Lock
1927
1928 def f(l):
1929 ... do something using "l" ...
1930
1931 if __name__ == '__main__':
1932 lock = Lock()
1933 for i in range(10):
1934 Process(target=f, args=(lock,)).start()
1935
1936
1937Windows
1938~~~~~~~
1939
1940Since Windows lacks :func:`os.fork` it has a few extra restrictions:
1941
1942More picklability
1943
1944 Ensure that all arguments to :meth:`Process.__init__` are picklable. This
1945 means, in particular, that bound or unbound methods cannot be used directly
1946 as the ``target`` argument on Windows --- just define a function and use
1947 that instead.
1948
1949 Also, if you subclass :class:`Process` then make sure that instances will be
1950 picklable when the :meth:`Process.start` method is called.
1951
1952Global variables
1953
1954 Bear in mind that if code run in a child process tries to access a global
1955 variable, then the value it sees (if any) may not be the same as the value
1956 in the parent process at the time that :meth:`Process.start` was called.
1957
1958 However, global variables which are just module level constants cause no
1959 problems.
1960
1961Safe importing of main module
1962
1963 Make sure that the main module can be safely imported by a new Python
1964 interpreter without causing unintended side effects (such a starting a new
1965 process).
1966
1967 For example, under Windows running the following module would fail with a
1968 :exc:`RuntimeError`::
1969
1970 from multiprocessing import Process
1971
1972 def foo():
1973 print 'hello'
1974
1975 p = Process(target=foo)
1976 p.start()
1977
1978 Instead one should protect the "entry point" of the program by using ``if
1979 __name__ == '__main__':`` as follows::
1980
1981 from multiprocessing import Process, freeze_support
1982
1983 def foo():
1984 print 'hello'
1985
1986 if __name__ == '__main__':
1987 freeze_support()
1988 p = Process(target=foo)
1989 p.start()
1990
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001991 (The ``freeze_support()`` line can be omitted if the program will be run
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001992 normally instead of frozen.)
1993
1994 This allows the newly spawned Python interpreter to safely import the module
1995 and then run the module's ``foo()`` function.
1996
1997 Similar restrictions apply if a pool or manager is created in the main
1998 module.
1999
2000
2001.. _multiprocessing-examples:
2002
2003Examples
2004--------
2005
2006Demonstration of how to create and use customized managers and proxies:
2007
2008.. literalinclude:: ../includes/mp_newtype.py
2009
2010
2011Using :class:`Pool`:
2012
2013.. literalinclude:: ../includes/mp_pool.py
2014
2015
2016Synchronization types like locks, conditions and queues:
2017
2018.. literalinclude:: ../includes/mp_synchronize.py
2019
2020
2021An showing how to use queues to feed tasks to a collection of worker process and
2022collect the results:
2023
2024.. literalinclude:: ../includes/mp_workers.py
2025
2026
2027An example of how a pool of worker processes can each run a
2028:class:`SimpleHTTPServer.HttpServer` instance while sharing a single listening
2029socket.
2030
2031.. literalinclude:: ../includes/mp_webserver.py
2032
2033
2034Some simple benchmarks comparing :mod:`multiprocessing` with :mod:`threading`:
2035
2036.. literalinclude:: ../includes/mp_benchmarks.py
2037
2038An example/demo of how to use the :class:`managers.SyncManager`, :class:`Process`
2039and others to build a system which can distribute processes and work via a
2040distributed queue to a "cluster" of machines on a network, accessible via SSH.
2041You will need to have private key authentication for all hosts configured for
2042this to work.
2043
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00002044.. literalinclude:: ../includes/mp_distributing.py