blob: 4b1196bec685149a578bb9d648f3b615522a65ae [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
173 holds python objects and allows other processes to manipulate them using
174 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
456 process may hang on exit when it tries to join all it non-daemonic children.
457
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
537 :class:`Queue.Queue` which are usually unnecessary:
538
539 .. method:: close()
540
541 Indicate that no more data will be put on this queue by the current
542 process. The background thread will quit once it has flushed all buffered
543 data to the pipe. This is called automatically when the queue is garbage
544 collected.
545
546 .. method:: join_thread()
547
548 Join the background thread. This can only be used after :meth:`close` has
549 been called. It blocks until the background thread exits, ensuring that
550 all data in the buffer has been flushed to the pipe.
551
552 By default if a process is not the creator of the queue then on exit it
553 will attempt to join the queue's background thread. The process can call
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000554 :meth:`cancel_join_thread` to make :meth:`join_thread` do nothing.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000555
556 .. method:: cancel_join_thread()
557
558 Prevent :meth:`join_thread` from blocking. In particular, this prevents
559 the background thread from being joined automatically when the process
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000560 exits -- see :meth:`join_thread`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000561
562
563.. class:: JoinableQueue([maxsize])
564
565 :class:`JoinableQueue`, a :class:`Queue` subclass, is a queue which
566 additionally has :meth:`task_done` and :meth:`join` methods.
567
568 .. method:: task_done()
569
570 Indicate that a formerly enqueued task is complete. Used by queue consumer
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000571 threads. For each :meth:`~Queue.get` used to fetch a task, a subsequent
572 call to :meth:`task_done` tells the queue that the processing on the task
573 is complete.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000574
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000575 If a :meth:`~Queue.join` is currently blocking, it will resume when all
576 items have been processed (meaning that a :meth:`task_done` call was
577 received for every item that had been :meth:`~Queue.put` into the queue).
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000578
579 Raises a :exc:`ValueError` if called more times than there were items
580 placed in the queue.
581
582
583 .. method:: join()
584
585 Block until all items in the queue have been gotten and processed.
586
587 The count of unfinished tasks goes up whenever an item is added to the
588 queue. The count goes down whenever a consumer thread calls
589 :meth:`task_done` to indicate that the item was retrieved and all work on
590 it is complete. When the count of unfinished tasks drops to zero,
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000591 :meth:`~Queue.join` unblocks.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000592
593
594Miscellaneous
595~~~~~~~~~~~~~
596
597.. function:: active_children()
598
599 Return list of all live children of the current process.
600
601 Calling this has the side affect of "joining" any processes which have
602 already finished.
603
604.. function:: cpu_count()
605
606 Return the number of CPUs in the system. May raise
607 :exc:`NotImplementedError`.
608
609.. function:: current_process()
610
611 Return the :class:`Process` object corresponding to the current process.
612
613 An analogue of :func:`threading.current_thread`.
614
615.. function:: freeze_support()
616
617 Add support for when a program which uses :mod:`multiprocessing` has been
618 frozen to produce a Windows executable. (Has been tested with **py2exe**,
619 **PyInstaller** and **cx_Freeze**.)
620
621 One needs to call this function straight after the ``if __name__ ==
622 '__main__'`` line of the main module. For example::
623
624 from multiprocessing import Process, freeze_support
625
626 def f():
627 print 'hello world!'
628
629 if __name__ == '__main__':
630 freeze_support()
631 Process(target=f).start()
632
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000633 If the ``freeze_support()`` line is missed out then trying to run the frozen
634 executable will raise :exc:`RuntimeError`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000635
636 If the module is being run normally by the Python interpreter then
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000637 :func:`freeze_support` has no effect.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000638
639.. function:: set_executable()
640
641 Sets the path of the python interpreter to use when starting a child process.
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000642 (By default :data:`sys.executable` is used). Embedders will probably need to
643 do some thing like ::
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000644
645 setExecutable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
646
647 before they can create child processes. (Windows only)
648
649
650.. note::
651
652 :mod:`multiprocessing` contains no analogues of
653 :func:`threading.active_count`, :func:`threading.enumerate`,
654 :func:`threading.settrace`, :func:`threading.setprofile`,
655 :class:`threading.Timer`, or :class:`threading.local`.
656
657
658Connection Objects
659~~~~~~~~~~~~~~~~~~
660
661Connection objects allow the sending and receiving of picklable objects or
662strings. They can be thought of as message oriented connected sockets.
663
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000664Connection objects usually created using :func:`Pipe` -- see also
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000665:ref:`multiprocessing-listeners-clients`.
666
667.. class:: Connection
668
669 .. method:: send(obj)
670
671 Send an object to the other end of the connection which should be read
672 using :meth:`recv`.
673
674 The object must be picklable.
675
676 .. method:: recv()
677
678 Return an object sent from the other end of the connection using
679 :meth:`send`. Raises :exc:`EOFError` if there is nothing left to receive
680 and the other end was closed.
681
682 .. method:: fileno()
683
684 Returns the file descriptor or handle used by the connection.
685
686 .. method:: close()
687
688 Close the connection.
689
690 This is called automatically when the connection is garbage collected.
691
692 .. method:: poll([timeout])
693
694 Return whether there is any data available to be read.
695
696 If *timeout* is not specified then it will return immediately. If
697 *timeout* is a number then this specifies the maximum time in seconds to
698 block. If *timeout* is ``None`` then an infinite timeout is used.
699
700 .. method:: send_bytes(buffer[, offset[, size]])
701
702 Send byte data from an object supporting the buffer interface as a
703 complete message.
704
705 If *offset* is given then data is read from that position in *buffer*. If
706 *size* is given then that many bytes will be read from buffer.
707
708 .. method:: recv_bytes([maxlength])
709
710 Return a complete message of byte data sent from the other end of the
711 connection as a string. Raises :exc:`EOFError` if there is nothing left
712 to receive and the other end has closed.
713
714 If *maxlength* is specified and the message is longer than *maxlength*
715 then :exc:`IOError` is raised and the connection will no longer be
716 readable.
717
718 .. method:: recv_bytes_into(buffer[, offset])
719
720 Read into *buffer* a complete message of byte data sent from the other end
721 of the connection and return the number of bytes in the message. Raises
722 :exc:`EOFError` if there is nothing left to receive and the other end was
723 closed.
724
725 *buffer* must be an object satisfying the writable buffer interface. If
726 *offset* is given then the message will be written into the buffer from
727 *that position. Offset must be a non-negative integer less than the
728 *length of *buffer* (in bytes).
729
730 If the buffer is too short then a :exc:`BufferTooShort` exception is
731 raised and the complete message is available as ``e.args[0]`` where ``e``
732 is the exception instance.
733
734
735For example:
736
737 >>> from multiprocessing import Pipe
738 >>> a, b = Pipe()
739 >>> a.send([1, 'hello', None])
740 >>> b.recv()
741 [1, 'hello', None]
742 >>> b.send_bytes('thank you')
743 >>> a.recv_bytes()
744 'thank you'
745 >>> import array
746 >>> arr1 = array.array('i', range(5))
747 >>> arr2 = array.array('i', [0] * 10)
748 >>> a.send_bytes(arr1)
749 >>> count = b.recv_bytes_into(arr2)
750 >>> assert count == len(arr1) * arr1.itemsize
751 >>> arr2
752 array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])
753
754
755.. warning::
756
757 The :meth:`Connection.recv` method automatically unpickles the data it
758 receives, which can be a security risk unless you can trust the process
759 which sent the message.
760
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000761 Therefore, unless the connection object was produced using :func:`Pipe` you
762 should only use the :meth:`~Connection.recv` and :meth:`~Connection.send`
763 methods after performing some sort of authentication. See
764 :ref:`multiprocessing-auth-keys`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000765
766.. warning::
767
768 If a process is killed while it is trying to read or write to a pipe then
769 the data in the pipe is likely to become corrupted, because it may become
770 impossible to be sure where the message boundaries lie.
771
772
773Synchronization primitives
774~~~~~~~~~~~~~~~~~~~~~~~~~~
775
776Generally synchronization primitives are not as necessary in a multiprocess
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000777program as they are in a mulithreaded program. See the documentation for
778:mod:`threading` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000779
780Note that one can also create synchronization primitives by using a manager
781object -- see :ref:`multiprocessing-managers`.
782
783.. class:: BoundedSemaphore([value])
784
785 A bounded semaphore object: a clone of :class:`threading.BoundedSemaphore`.
786
Benjamin Peterson90f36732008-07-12 20:16:19 +0000787 (On Mac OSX this is indistinguishable from :class:`Semaphore` because
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000788 ``sem_getvalue()`` is not implemented on that platform).
789
790.. class:: Condition([lock])
791
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000792 A condition variable: a clone of :class:`threading.Condition`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000793
794 If *lock* is specified then it should be a :class:`Lock` or :class:`RLock`
795 object from :mod:`multiprocessing`.
796
797.. class:: Event()
798
799 A clone of :class:`threading.Event`.
800
801.. class:: Lock()
802
803 A non-recursive lock object: a clone of :class:`threading.Lock`.
804
805.. class:: RLock()
806
807 A recursive lock object: a clone of :class:`threading.RLock`.
808
809.. class:: Semaphore([value])
810
811 A bounded semaphore object: a clone of :class:`threading.Semaphore`.
812
813.. note::
814
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000815 The :meth:`acquire` method of :class:`BoundedSemaphore`, :class:`Lock`,
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000816 :class:`RLock` and :class:`Semaphore` has a timeout parameter not supported
817 by the equivalents in :mod:`threading`. The signature is
818 ``acquire(block=True, timeout=None)`` with keyword parameters being
819 acceptable. If *block* is ``True`` and *timeout* is not ``None`` then it
820 specifies a timeout in seconds. If *block* is ``False`` then *timeout* is
821 ignored.
822
823.. note::
824
825 If the SIGINT signal generated by Ctrl-C arrives while the main thread is
826 blocked by a call to :meth:`BoundedSemaphore.acquire`, :meth:`Lock.acquire`,
827 :meth:`RLock.acquire`, :meth:`Semaphore.acquire`, :meth:`Condition.acquire`
828 or :meth:`Condition.wait` then the call will be immediately interrupted and
829 :exc:`KeyboardInterrupt` will be raised.
830
831 This differs from the behaviour of :mod:`threading` where SIGINT will be
832 ignored while the equivalent blocking calls are in progress.
833
834
835Shared :mod:`ctypes` Objects
836~~~~~~~~~~~~~~~~~~~~~~~~~~~~
837
838It is possible to create shared objects using shared memory which can be
839inherited by child processes.
840
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000841.. function:: Value(typecode_or_type[, *args, lock]])
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000842
843 Return a :mod:`ctypes` object allocated from shared memory. By default the
844 return value is actually a synchronized wrapper for the object.
845
846 *typecode_or_type* determines the type of the returned object: it is either a
847 ctypes type or a one character typecode of the kind used by the :mod:`array`
848 module. *\*args* is passed on to the constructor for the type.
849
850 If *lock* is ``True`` (the default) then a new lock object is created to
851 synchronize access to the value. If *lock* is a :class:`Lock` or
852 :class:`RLock` object then that will be used to synchronize access to the
853 value. If *lock* is ``False`` then access to the returned object will not be
854 automatically protected by a lock, so it will not necessarily be
855 "process-safe".
856
857 Note that *lock* is a keyword-only argument.
858
859.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True)
860
861 Return a ctypes array allocated from shared memory. By default the return
862 value is actually a synchronized wrapper for the array.
863
864 *typecode_or_type* determines the type of the elements of the returned array:
865 it is either a ctypes type or a one character typecode of the kind used by
866 the :mod:`array` module. If *size_or_initializer* is an integer, then it
867 determines the length of the array, and the array will be initially zeroed.
868 Otherwise, *size_or_initializer* is a sequence which is used to initialize
869 the array and whose length determines the length of the array.
870
871 If *lock* is ``True`` (the default) then a new lock object is created to
872 synchronize access to the value. If *lock* is a :class:`Lock` or
873 :class:`RLock` object then that will be used to synchronize access to the
874 value. If *lock* is ``False`` then access to the returned object will not be
875 automatically protected by a lock, so it will not necessarily be
876 "process-safe".
877
878 Note that *lock* is a keyword only argument.
879
880 Note that an array of :data:`ctypes.c_char` has *value* and *rawvalue*
881 attributes which allow one to use it to store and retrieve strings.
882
883
884The :mod:`multiprocessing.sharedctypes` module
885>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
886
887.. module:: multiprocessing.sharedctypes
888 :synopsis: Allocate ctypes objects from shared memory.
889
890The :mod:`multiprocessing.sharedctypes` module provides functions for allocating
891:mod:`ctypes` objects from shared memory which can be inherited by child
892processes.
893
894.. note::
895
Benjamin Peterson90f36732008-07-12 20:16:19 +0000896 Although it is possible to store a pointer in shared memory remember that
897 this will refer to a location in the address space of a specific process.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000898 However, the pointer is quite likely to be invalid in the context of a second
899 process and trying to dereference the pointer from the second process may
900 cause a crash.
901
902.. function:: RawArray(typecode_or_type, size_or_initializer)
903
904 Return a ctypes array allocated from shared memory.
905
906 *typecode_or_type* determines the type of the elements of the returned array:
907 it is either a ctypes type or a one character typecode of the kind used by
908 the :mod:`array` module. If *size_or_initializer* is an integer then it
909 determines the length of the array, and the array will be initially zeroed.
910 Otherwise *size_or_initializer* is a sequence which is used to initialize the
911 array and whose length determines the length of the array.
912
913 Note that setting and getting an element is potentially non-atomic -- use
914 :func:`Array` instead to make sure that access is automatically synchronized
915 using a lock.
916
917.. function:: RawValue(typecode_or_type, *args)
918
919 Return a ctypes object allocated from shared memory.
920
921 *typecode_or_type* determines the type of the returned object: it is either a
922 ctypes type or a one character typecode of the kind used by the :mod:`array`
923 module. */*args* is passed on to the constructor for the type.
924
925 Note that setting and getting the value is potentially non-atomic -- use
926 :func:`Value` instead to make sure that access is automatically synchronized
927 using a lock.
928
929 Note that an array of :data:`ctypes.c_char` has ``value`` and ``rawvalue``
930 attributes which allow one to use it to store and retrieve strings -- see
931 documentation for :mod:`ctypes`.
932
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000933.. function:: Array(typecode_or_type, size_or_initializer[, *args[, lock]])
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000934
935 The same as :func:`RawArray` except that depending on the value of *lock* a
936 process-safe synchronization wrapper may be returned instead of a raw ctypes
937 array.
938
939 If *lock* is ``True`` (the default) then a new lock object is created to
940 synchronize access to the value. If *lock* is a :class:`Lock` or
941 :class:`RLock` object then that will be used to synchronize access to the
942 value. If *lock* is ``False`` then access to the returned object will not be
943 automatically protected by a lock, so it will not necessarily be
944 "process-safe".
945
946 Note that *lock* is a keyword-only argument.
947
948.. function:: Value(typecode_or_type, *args[, lock])
949
950 The same as :func:`RawValue` except that depending on the value of *lock* a
951 process-safe synchronization wrapper may be returned instead of a raw ctypes
952 object.
953
954 If *lock* is ``True`` (the default) then a new lock object is created to
955 synchronize access to the value. If *lock* is a :class:`Lock` or
956 :class:`RLock` object then that will be used to synchronize access to the
957 value. If *lock* is ``False`` then access to the returned object will not be
958 automatically protected by a lock, so it will not necessarily be
959 "process-safe".
960
961 Note that *lock* is a keyword-only argument.
962
963.. function:: copy(obj)
964
965 Return a ctypes object allocated from shared memory which is a copy of the
966 ctypes object *obj*.
967
968.. function:: synchronized(obj[, lock])
969
970 Return a process-safe wrapper object for a ctypes object which uses *lock* to
971 synchronize access. If *lock* is ``None`` (the default) then a
972 :class:`multiprocessing.RLock` object is created automatically.
973
974 A synchronized wrapper will have two methods in addition to those of the
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000975 object it wraps: :meth:`get_obj` returns the wrapped object and
976 :meth:`get_lock` returns the lock object used for synchronization.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000977
978 Note that accessing the ctypes object through the wrapper can be a lot slower
Benjamin Peterson910c2ab2008-06-27 23:22:06 +0000979 than accessing the raw ctypes object.
Benjamin Peterson190d56e2008-06-11 02:40:25 +0000980
981
982The table below compares the syntax for creating shared ctypes objects from
983shared memory with the normal ctypes syntax. (In the table ``MyStruct`` is some
984subclass of :class:`ctypes.Structure`.)
985
986==================== ========================== ===========================
987ctypes sharedctypes using type sharedctypes using typecode
988==================== ========================== ===========================
989c_double(2.4) RawValue(c_double, 2.4) RawValue('d', 2.4)
990MyStruct(4, 6) RawValue(MyStruct, 4, 6)
991(c_short * 7)() RawArray(c_short, 7) RawArray('h', 7)
992(c_int * 3)(9, 2, 8) RawArray(c_int, (9, 2, 8)) RawArray('i', (9, 2, 8))
993==================== ========================== ===========================
994
995
996Below is an example where a number of ctypes objects are modified by a child
997process::
998
999 from multiprocessing import Process, Lock
1000 from multiprocessing.sharedctypes import Value, Array
1001 from ctypes import Structure, c_double
1002
1003 class Point(Structure):
1004 _fields_ = [('x', c_double), ('y', c_double)]
1005
1006 def modify(n, x, s, A):
1007 n.value **= 2
1008 x.value **= 2
1009 s.value = s.value.upper()
1010 for a in A:
1011 a.x **= 2
1012 a.y **= 2
1013
1014 if __name__ == '__main__':
1015 lock = Lock()
1016
1017 n = Value('i', 7)
1018 x = Value(ctypes.c_double, 1.0/3.0, lock=False)
1019 s = Array('c', 'hello world', lock=lock)
1020 A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)
1021
1022 p = Process(target=modify, args=(n, x, s, A))
1023 p.start()
1024 p.join()
1025
1026 print n.value
1027 print x.value
1028 print s.value
1029 print [(a.x, a.y) for a in A]
1030
1031
1032.. highlightlang:: none
1033
1034The results printed are ::
1035
1036 49
1037 0.1111111111111111
1038 HELLO WORLD
1039 [(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]
1040
1041.. highlightlang:: python
1042
1043
1044.. _multiprocessing-managers:
1045
1046Managers
1047~~~~~~~~
1048
1049Managers provide a way to create data which can be shared between different
1050processes. A manager object controls a server process which manages *shared
1051objects*. Other processes can access the shared objects by using proxies.
1052
1053.. function:: multiprocessing.Manager()
1054
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001055 Returns a started :class:`~multiprocessing.managers.SyncManager` object which
1056 can be used for sharing objects between processes. The returned manager
1057 object corresponds to a spawned child process and has methods which will
1058 create shared objects and return corresponding proxies.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001059
1060.. module:: multiprocessing.managers
1061 :synopsis: Share data between process with shared objects.
1062
1063Manager processes will be shutdown as soon as they are garbage collected or
1064their parent process exits. The manager classes are defined in the
1065:mod:`multiprocessing.managers` module:
1066
1067.. class:: BaseManager([address[, authkey]])
1068
1069 Create a BaseManager object.
1070
1071 Once created one should call :meth:`start` or :meth:`serve_forever` to ensure
1072 that the manager object refers to a started manager process.
1073
1074 *address* is the address on which the manager process listens for new
1075 connections. If *address* is ``None`` then an arbitrary one is chosen.
1076
1077 *authkey* is the authentication key which will be used to check the validity
1078 of incoming connections to the server process. If *authkey* is ``None`` then
1079 ``current_process().get_auth_key()``. Otherwise *authkey* is used and it
1080 must be a string.
1081
1082 .. method:: start()
1083
1084 Start a subprocess to start the manager.
1085
1086 .. method:: server_forever()
1087
1088 Run the server in the current process.
1089
1090 .. method:: from_address(address, authkey)
1091
1092 A class method which creates a manager object referring to a pre-existing
1093 server process which is using the given address and authentication key.
1094
1095 .. method:: shutdown()
1096
1097 Stop the process used by the manager. This is only available if
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001098 :meth:`start` has been used to start the server process.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001099
1100 This can be called multiple times.
1101
1102 .. method:: register(typeid[, callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]])
1103
1104 A classmethod which can be used for registering a type or callable with
1105 the manager class.
1106
1107 *typeid* is a "type identifier" which is used to identify a particular
1108 type of shared object. This must be a string.
1109
1110 *callable* is a callable used for creating objects for this type
1111 identifier. If a manager instance will be created using the
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001112 :meth:`from_address` classmethod or if the *create_method* argument is
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001113 ``False`` then this can be left as ``None``.
1114
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001115 *proxytype* is a subclass of :class:`BaseProxy` which is used to create
1116 proxies for shared objects with this *typeid*. If ``None`` then a proxy
1117 class is created automatically.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001118
1119 *exposed* is used to specify a sequence of method names which proxies for
1120 this typeid should be allowed to access using
1121 :meth:`BaseProxy._callMethod`. (If *exposed* is ``None`` then
1122 :attr:`proxytype._exposed_` is used instead if it exists.) In the case
1123 where no exposed list is specified, all "public methods" of the shared
1124 object will be accessible. (Here a "public method" means any attribute
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001125 which has a :meth:`__call__` method and whose name does not begin with
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001126 ``'_'``.)
1127
1128 *method_to_typeid* is a mapping used to specify the return type of those
1129 exposed methods which should return a proxy. It maps method names to
1130 typeid strings. (If *method_to_typeid* is ``None`` then
1131 :attr:`proxytype._method_to_typeid_` is used instead if it exists.) If a
1132 method's name is not a key of this mapping or if the mapping is ``None``
1133 then the object returned by the method will be copied by value.
1134
1135 *create_method* determines whether a method should be created with name
1136 *typeid* which can be used to tell the server process to create a new
1137 shared object and return a proxy for it. By default it is ``True``.
1138
1139 :class:`BaseManager` instances also have one read-only property:
1140
1141 .. attribute:: address
1142
1143 The address used by the manager.
1144
1145
1146.. class:: SyncManager
1147
1148 A subclass of :class:`BaseManager` which can be used for the synchronization
1149 of processes. Objects of this type are returned by
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001150 :func:`multiprocessing.Manager`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001151
1152 It also supports creation of shared lists and dictionaries.
1153
1154 .. method:: BoundedSemaphore([value])
1155
1156 Create a shared :class:`threading.BoundedSemaphore` object and return a
1157 proxy for it.
1158
1159 .. method:: Condition([lock])
1160
1161 Create a shared :class:`threading.Condition` object and return a proxy for
1162 it.
1163
1164 If *lock* is supplied then it should be a proxy for a
1165 :class:`threading.Lock` or :class:`threading.RLock` object.
1166
1167 .. method:: Event()
1168
1169 Create a shared :class:`threading.Event` object and return a proxy for it.
1170
1171 .. method:: Lock()
1172
1173 Create a shared :class:`threading.Lock` object and return a proxy for it.
1174
1175 .. method:: Namespace()
1176
1177 Create a shared :class:`Namespace` object and return a proxy for it.
1178
1179 .. method:: Queue([maxsize])
1180
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001181 Create a shared :class:`Queue.Queue` object and return a proxy for it.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001182
1183 .. method:: RLock()
1184
1185 Create a shared :class:`threading.RLock` object and return a proxy for it.
1186
1187 .. method:: Semaphore([value])
1188
1189 Create a shared :class:`threading.Semaphore` object and return a proxy for
1190 it.
1191
1192 .. method:: Array(typecode, sequence)
1193
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001194 Create an array and return a proxy for it.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001195
1196 .. method:: Value(typecode, value)
1197
1198 Create an object with a writable ``value`` attribute and return a proxy
1199 for it.
1200
1201 .. method:: dict()
1202 dict(mapping)
1203 dict(sequence)
1204
1205 Create a shared ``dict`` object and return a proxy for it.
1206
1207 .. method:: list()
1208 list(sequence)
1209
1210 Create a shared ``list`` object and return a proxy for it.
1211
1212
1213Namespace objects
1214>>>>>>>>>>>>>>>>>
1215
1216A namespace object has no public methods, but does have writable attributes.
1217Its representation shows the values of its attributes.
1218
1219However, when using a proxy for a namespace object, an attribute beginning with
1220``'_'`` will be an attribute of the proxy and not an attribute of the referent::
1221
1222 >>> manager = multiprocessing.Manager()
1223 >>> Global = manager.Namespace()
1224 >>> Global.x = 10
1225 >>> Global.y = 'hello'
1226 >>> Global._z = 12.3 # this is an attribute of the proxy
1227 >>> print Global
1228 Namespace(x=10, y='hello')
1229
1230
1231Customized managers
1232>>>>>>>>>>>>>>>>>>>
1233
1234To create one's own manager, one creates a subclass of :class:`BaseManager` and
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001235use the :meth:`~BaseManager.resgister` classmethod to register new types or
1236callables with the manager class. For example::
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001237
1238 from multiprocessing.managers import BaseManager
1239
1240 class MathsClass(object):
1241 def add(self, x, y):
1242 return x + y
1243 def mul(self, x, y):
1244 return x * y
1245
1246 class MyManager(BaseManager):
1247 pass
1248
1249 MyManager.register('Maths', MathsClass)
1250
1251 if __name__ == '__main__':
1252 manager = MyManager()
1253 manager.start()
1254 maths = manager.Maths()
1255 print maths.add(4, 3) # prints 7
1256 print maths.mul(7, 8) # prints 56
1257
1258
1259Using a remote manager
1260>>>>>>>>>>>>>>>>>>>>>>
1261
1262It is possible to run a manager server on one machine and have clients use it
1263from other machines (assuming that the firewalls involved allow it).
1264
1265Running the following commands creates a server for a single shared queue which
1266remote clients can access::
1267
1268 >>> from multiprocessing.managers import BaseManager
1269 >>> import Queue
1270 >>> queue = Queue.Queue()
1271 >>> class QueueManager(BaseManager): pass
1272 ...
1273 >>> QueueManager.register('getQueue', callable=lambda:queue)
1274 >>> m = QueueManager(address=('', 50000), authkey='abracadabra')
1275 >>> m.serveForever()
1276
1277One client can access the server as follows::
1278
1279 >>> from multiprocessing.managers import BaseManager
1280 >>> class QueueManager(BaseManager): pass
1281 ...
1282 >>> QueueManager.register('getQueue')
1283 >>> m = QueueManager.from_address(address=('foo.bar.org', 50000),
1284 >>> authkey='abracadabra')
1285 >>> queue = m.getQueue()
1286 >>> queue.put('hello')
1287
1288Another client can also use it::
1289
1290 >>> from multiprocessing.managers import BaseManager
1291 >>> class QueueManager(BaseManager): pass
1292 ...
1293 >>> QueueManager.register('getQueue')
1294 >>> m = QueueManager.from_address(address=('foo.bar.org', 50000), authkey='abracadabra')
1295 >>> queue = m.getQueue()
1296 >>> queue.get()
1297 'hello'
1298
1299
1300Proxy Objects
1301~~~~~~~~~~~~~
1302
1303A proxy is an object which *refers* to a shared object which lives (presumably)
1304in a different process. The shared object is said to be the *referent* of the
1305proxy. Multiple proxy objects may have the same referent.
1306
1307A proxy object has methods which invoke corresponding methods of its referent
1308(although not every method of the referent will necessarily be available through
1309the proxy). A proxy can usually be used in most of the same ways that its
1310referent can::
1311
1312 >>> from multiprocessing import Manager
1313 >>> manager = Manager()
1314 >>> l = manager.list([i*i for i in range(10)])
1315 >>> print l
1316 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
1317 >>> print repr(l)
1318 <ListProxy object, typeid 'list' at 0xb799974c>
1319 >>> l[4]
1320 16
1321 >>> l[2:5]
1322 [4, 9, 16]
1323
1324Notice that applying :func:`str` to a proxy will return the representation of
1325the referent, whereas applying :func:`repr` will return the representation of
1326the proxy.
1327
1328An important feature of proxy objects is that they are picklable so they can be
1329passed between processes. Note, however, that if a proxy is sent to the
1330corresponding manager's process then unpickling it will produce the referent
1331itself. This means, for example, that one shared object can contain a second::
1332
1333 >>> a = manager.list()
1334 >>> b = manager.list()
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001335 >>> a.append(b) # referent of a now contains referent of b
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001336 >>> print a, b
1337 [[]] []
1338 >>> b.append('hello')
1339 >>> print a, b
1340 [['hello']] ['hello']
1341
1342.. note::
1343
1344 The proxy types in :mod:`multiprocessing` do nothing to support comparisons
1345 by value. So, for instance, ::
1346
1347 manager.list([1,2,3]) == [1,2,3]
1348
1349 will return ``False``. One should just use a copy of the referent instead
1350 when making comparisons.
1351
1352.. class:: BaseProxy
1353
1354 Proxy objects are instances of subclasses of :class:`BaseProxy`.
1355
1356 .. method:: _call_method(methodname[, args[, kwds]])
1357
1358 Call and return the result of a method of the proxy's referent.
1359
1360 If ``proxy`` is a proxy whose referent is ``obj`` then the expression ::
1361
1362 proxy._call_method(methodname, args, kwds)
1363
1364 will evaluate the expression ::
1365
1366 getattr(obj, methodname)(*args, **kwds)
1367
1368 in the manager's process.
1369
1370 The returned value will be a copy of the result of the call or a proxy to
1371 a new shared object -- see documentation for the *method_to_typeid*
1372 argument of :meth:`BaseManager.register`.
1373
1374 If an exception is raised by the call, then then is re-raised by
1375 :meth:`_call_method`. If some other exception is raised in the manager's
1376 process then this is converted into a :exc:`RemoteError` exception and is
1377 raised by :meth:`_call_method`.
1378
1379 Note in particular that an exception will be raised if *methodname* has
1380 not been *exposed*
1381
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001382 An example of the usage of :meth:`_call_method`::
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001383
1384 >>> l = manager.list(range(10))
1385 >>> l._call_method('__len__')
1386 10
1387 >>> l._call_method('__getslice__', (2, 7)) # equiv to `l[2:7]`
1388 [2, 3, 4, 5, 6]
1389 >>> l._call_method('__getitem__', (20,)) # equiv to `l[20]`
1390 Traceback (most recent call last):
1391 ...
1392 IndexError: list index out of range
1393
1394 .. method:: _get_value()
1395
1396 Return a copy of the referent.
1397
1398 If the referent is unpicklable then this will raise an exception.
1399
1400 .. method:: __repr__
1401
1402 Return a representation of the proxy object.
1403
1404 .. method:: __str__
1405
1406 Return the representation of the referent.
1407
1408
1409Cleanup
1410>>>>>>>
1411
1412A proxy object uses a weakref callback so that when it gets garbage collected it
1413deregisters itself from the manager which owns its referent.
1414
1415A shared object gets deleted from the manager process when there are no longer
1416any proxies referring to it.
1417
1418
1419Process Pools
1420~~~~~~~~~~~~~
1421
1422.. module:: multiprocessing.pool
1423 :synopsis: Create pools of processes.
1424
1425One can create a pool of processes which will carry out tasks submitted to it
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001426with the :class:`Pool` class.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001427
1428.. class:: multiprocessing.Pool([processes[, initializer[, initargs]]])
1429
1430 A process pool object which controls a pool of worker processes to which jobs
1431 can be submitted. It supports asynchronous results with timeouts and
1432 callbacks and has a parallel map implementation.
1433
1434 *processes* is the number of worker processes to use. If *processes* is
1435 ``None`` then the number returned by :func:`cpu_count` is used. If
1436 *initializer* is not ``None`` then each worker process will call
1437 ``initializer(*initargs)`` when it starts.
1438
1439 .. method:: apply(func[, args[, kwds]])
1440
1441 Equivalent of the :func:`apply` builtin function. It blocks till the
1442 result is ready.
1443
1444 .. method:: apply_async(func[, args[, kwds[, callback]]])
1445
1446 A variant of the :meth:`apply` method which returns a result object.
1447
1448 If *callback* is specified then it should be a callable which accepts a
1449 single argument. When the result becomes ready *callback* is applied to
1450 it (unless the call failed). *callback* should complete immediately since
1451 otherwise the thread which handles the results will get blocked.
1452
1453 .. method:: map(func, iterable[, chunksize])
1454
1455 A parallel equivalent of the :func:`map` builtin function. It blocks till
1456 the result is ready.
1457
1458 This method chops the iterable into a number of chunks which it submits to
1459 the process pool as separate tasks. The (approximate) size of these
1460 chunks can be specified by setting *chunksize* to a positive integer.
1461
1462 .. method:: map_async(func, iterable[, chunksize[, callback]])
1463
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001464 A variant of the :meth:`map` method which returns a result object.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001465
1466 If *callback* is specified then it should be a callable which accepts a
1467 single argument. When the result becomes ready *callback* is applied to
1468 it (unless the call failed). *callback* should complete immediately since
1469 otherwise the thread which handles the results will get blocked.
1470
1471 .. method:: imap(func, iterable[, chunksize])
1472
1473 An equivalent of :func:`itertools.imap`.
1474
1475 The *chunksize* argument is the same as the one used by the :meth:`.map`
1476 method. For very long iterables using a large value for *chunksize* can
1477 make make the job complete **much** faster than using the default value of
1478 ``1``.
1479
1480 Also if *chunksize* is ``1`` then the :meth:`next` method of the iterator
1481 returned by the :meth:`imap` method has an optional *timeout* parameter:
1482 ``next(timeout)`` will raise :exc:`multiprocessing.TimeoutError` if the
1483 result cannot be returned within *timeout* seconds.
1484
1485 .. method:: imap_unordered(func, iterable[, chunksize])
1486
1487 The same as :meth:`imap` except that the ordering of the results from the
1488 returned iterator should be considered arbitrary. (Only when there is
1489 only one worker process is the order guaranteed to be "correct".)
1490
1491 .. method:: close()
1492
1493 Prevents any more tasks from being submitted to the pool. Once all the
1494 tasks have been completed the worker processes will exit.
1495
1496 .. method:: terminate()
1497
1498 Stops the worker processes immediately without completing outstanding
1499 work. When the pool object is garbage collected :meth:`terminate` will be
1500 called immediately.
1501
1502 .. method:: join()
1503
1504 Wait for the worker processes to exit. One must call :meth:`close` or
1505 :meth:`terminate` before using :meth:`join`.
1506
1507
1508.. class:: AsyncResult
1509
1510 The class of the result returned by :meth:`Pool.apply_async` and
1511 :meth:`Pool.map_async`.
1512
1513 .. method:: get([timeout)
1514
1515 Return the result when it arrives. If *timeout* is not ``None`` and the
1516 result does not arrive within *timeout* seconds then
1517 :exc:`multiprocessing.TimeoutError` is raised. If the remote call raised
1518 an exception then that exception will be reraised by :meth:`get`.
1519
1520 .. method:: wait([timeout])
1521
1522 Wait until the result is available or until *timeout* seconds pass.
1523
1524 .. method:: ready()
1525
1526 Return whether the call has completed.
1527
1528 .. method:: successful()
1529
1530 Return whether the call completed without raising an exception. Will
1531 raise :exc:`AssertionError` if the result is not ready.
1532
1533The following example demonstrates the use of a pool::
1534
1535 from multiprocessing import Pool
1536
1537 def f(x):
1538 return x*x
1539
1540 if __name__ == '__main__':
1541 pool = Pool(processes=4) # start 4 worker processes
1542
1543 result = pool.applyAsync(f, (10,)) # evaluate "f(10)" asynchronously
1544 print result.get(timeout=1) # prints "100" unless your computer is *very* slow
1545
1546 print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
1547
1548 it = pool.imap(f, range(10))
1549 print it.next() # prints "0"
1550 print it.next() # prints "1"
1551 print it.next(timeout=1) # prints "4" unless your computer is *very* slow
1552
1553 import time
1554 result = pool.applyAsync(time.sleep, (10,))
1555 print result.get(timeout=1) # raises TimeoutError
1556
1557
1558.. _multiprocessing-listeners-clients:
1559
1560Listeners and Clients
1561~~~~~~~~~~~~~~~~~~~~~
1562
1563.. module:: multiprocessing.connection
1564 :synopsis: API for dealing with sockets.
1565
1566Usually message passing between processes is done using queues or by using
1567:class:`Connection` objects returned by :func:`Pipe`.
1568
1569However, the :mod:`multiprocessing.connection` module allows some extra
1570flexibility. It basically gives a high level message oriented API for dealing
1571with sockets or Windows named pipes, and also has support for *digest
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001572authentication* using the :mod:`hmac` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001573
1574
1575.. function:: deliver_challenge(connection, authkey)
1576
1577 Send a randomly generated message to the other end of the connection and wait
1578 for a reply.
1579
1580 If the reply matches the digest of the message using *authkey* as the key
1581 then a welcome message is sent to the other end of the connection. Otherwise
1582 :exc:`AuthenticationError` is raised.
1583
1584.. function:: answerChallenge(connection, authkey)
1585
1586 Receive a message, calculate the digest of the message using *authkey* as the
1587 key, and then send the digest back.
1588
1589 If a welcome message is not received, then :exc:`AuthenticationError` is
1590 raised.
1591
1592.. function:: Client(address[, family[, authenticate[, authkey]]])
1593
1594 Attempt to set up a connection to the listener which is using address
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001595 *address*, returning a :class:`~multiprocessing.Connection`.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001596
1597 The type of the connection is determined by *family* argument, but this can
1598 generally be omitted since it can usually be inferred from the format of
1599 *address*. (See :ref:`multiprocessing-address-formats`)
1600
1601 If *authentication* is ``True`` or *authkey* is a string then digest
1602 authentication is used. The key used for authentication will be either
1603 *authkey* or ``current_process().get_auth_key()`` if *authkey* is ``None``.
1604 If authentication fails then :exc:`AuthenticationError` is raised. See
1605 :ref:`multiprocessing-auth-keys`.
1606
1607.. class:: Listener([address[, family[, backlog[, authenticate[, authkey]]]]])
1608
1609 A wrapper for a bound socket or Windows named pipe which is 'listening' for
1610 connections.
1611
1612 *address* is the address to be used by the bound socket or named pipe of the
1613 listener object.
1614
1615 *family* is the type of socket (or named pipe) to use. This can be one of
1616 the strings ``'AF_INET'`` (for a TCP socket), ``'AF_UNIX'`` (for a Unix
1617 domain socket) or ``'AF_PIPE'`` (for a Windows named pipe). Of these only
1618 the first is guaranteed to be available. If *family* is ``None`` then the
1619 family is inferred from the format of *address*. If *address* is also
1620 ``None`` then a default is chosen. This default is the family which is
1621 assumed to be the fastest available. See
1622 :ref:`multiprocessing-address-formats`. Note that if *family* is
1623 ``'AF_UNIX'`` and address is ``None`` then the socket will be created in a
1624 private temporary directory created using :func:`tempfile.mkstemp`.
1625
1626 If the listener object uses a socket then *backlog* (1 by default) is passed
1627 to the :meth:`listen` method of the socket once it has been bound.
1628
1629 If *authenticate* is ``True`` (``False`` by default) or *authkey* is not
1630 ``None`` then digest authentication is used.
1631
1632 If *authkey* is a string then it will be used as the authentication key;
1633 otherwise it must be *None*.
1634
1635 If *authkey* is ``None`` and *authenticate* is ``True`` then
1636 ``current_process().get_auth_key()`` is used as the authentication key. If
1637 *authkey* is ``None`` and *authentication* is ``False`` then no
1638 authentication is done. If authentication fails then
1639 :exc:`AuthenticationError` is raised. See :ref:`multiprocessing-auth-keys`.
1640
1641 .. method:: accept()
1642
1643 Accept a connection on the bound socket or named pipe of the listener
1644 object and return a :class:`Connection` object. If authentication is
1645 attempted and fails, then :exc:`AuthenticationError` is raised.
1646
1647 .. method:: close()
1648
1649 Close the bound socket or named pipe of the listener object. This is
1650 called automatically when the listener is garbage collected. However it
1651 is advisable to call it explicitly.
1652
1653 Listener objects have the following read-only properties:
1654
1655 .. attribute:: address
1656
1657 The address which is being used by the Listener object.
1658
1659 .. attribute:: last_accepted
1660
1661 The address from which the last accepted connection came. If this is
1662 unavailable then it is ``None``.
1663
1664
1665The module defines two exceptions:
1666
1667.. exception:: AuthenticationError
1668
1669 Exception raised when there is an authentication error.
1670
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001671
1672**Examples**
1673
1674The following server code creates a listener which uses ``'secret password'`` as
1675an authentication key. It then waits for a connection and sends some data to
1676the client::
1677
1678 from multiprocessing.connection import Listener
1679 from array import array
1680
1681 address = ('localhost', 6000) # family is deduced to be 'AF_INET'
1682 listener = Listener(address, authkey='secret password')
1683
1684 conn = listener.accept()
1685 print 'connection accepted from', listener.last_accepted
1686
1687 conn.send([2.25, None, 'junk', float])
1688
1689 conn.send_bytes('hello')
1690
1691 conn.send_bytes(array('i', [42, 1729]))
1692
1693 conn.close()
1694 listener.close()
1695
1696The following code connects to the server and receives some data from the
1697server::
1698
1699 from multiprocessing.connection import Client
1700 from array import array
1701
1702 address = ('localhost', 6000)
1703 conn = Client(address, authkey='secret password')
1704
1705 print conn.recv() # => [2.25, None, 'junk', float]
1706
1707 print conn.recv_bytes() # => 'hello'
1708
1709 arr = array('i', [0, 0, 0, 0, 0])
1710 print conn.recv_bytes_into(arr) # => 8
1711 print arr # => array('i', [42, 1729, 0, 0, 0])
1712
1713 conn.close()
1714
1715
1716.. _multiprocessing-address-formats:
1717
1718Address Formats
1719>>>>>>>>>>>>>>>
1720
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001721* An ``'AF_INET'`` address is a tuple of the form ``(hostname, port)`` where
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001722 *hostname* is a string and *port* is an integer.
1723
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001724* An ``'AF_UNIX'`` address is a string representing a filename on the
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001725 filesystem.
1726
1727* An ``'AF_PIPE'`` address is a string of the form
1728 ``r'\\\\.\\pipe\\PipeName'``. To use :func:`Client` to connect to a named
1729 pipe on a remote computer called ServerName* one should use an address of the
1730 form ``r'\\\\ServerName\\pipe\\PipeName'`` instead.
1731
1732Note that any string beginning with two backslashes is assumed by default to be
1733an ``'AF_PIPE'`` address rather than an ``'AF_UNIX'`` address.
1734
1735
1736.. _multiprocessing-auth-keys:
1737
1738Authentication keys
1739~~~~~~~~~~~~~~~~~~~
1740
1741When one uses :meth:`Connection.recv`, the data received is automatically
1742unpickled. Unfortunately unpickling data from an untrusted source is a security
1743risk. Therefore :class:`Listener` and :func:`Client` use the :mod:`hmac` module
1744to provide digest authentication.
1745
1746An authentication key is a string which can be thought of as a password: once a
1747connection is established both ends will demand proof that the other knows the
1748authentication key. (Demonstrating that both ends are using the same key does
1749**not** involve sending the key over the connection.)
1750
1751If authentication is requested but do authentication key is specified then the
1752return value of ``current_process().get_auth_key`` is used (see
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001753:class:`~multiprocessing.Process`). This value will automatically inherited by
1754any :class:`~multiprocessing.Process` object that the current process creates.
1755This means that (by default) all processes of a multi-process program will share
1756a single authentication key which can be used when setting up connections
1757between the themselves.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001758
1759Suitable authentication keys can also be generated by using :func:`os.urandom`.
1760
1761
1762Logging
1763~~~~~~~
1764
1765Some support for logging is available. Note, however, that the :mod:`logging`
1766package does not use process shared locks so it is possible (depending on the
1767handler type) for messages from different processes to get mixed up.
1768
1769.. currentmodule:: multiprocessing
1770.. function:: get_logger()
1771
1772 Returns the logger used by :mod:`multiprocessing`. If necessary, a new one
1773 will be created.
1774
1775 When first created the logger has level :data:`logging.NOTSET` and has a
1776 handler which sends output to :data:`sys.stderr` using format
1777 ``'[%(levelname)s/%(processName)s] %(message)s'``. (The logger allows use of
1778 the non-standard ``'%(processName)s'`` format.) Message sent to this logger
Benjamin Peterson90f36732008-07-12 20:16:19 +00001779 will not by default propagate to the root logger.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001780
1781 Note that on Windows child processes will only inherit the level of the
1782 parent process's logger -- any other customization of the logger will not be
1783 inherited.
1784
1785Below is an example session with logging turned on::
1786
1787 >>> import processing, logging
1788 >>> logger = processing.getLogger()
1789 >>> logger.setLevel(logging.INFO)
1790 >>> logger.warning('doomed')
1791 [WARNING/MainProcess] doomed
1792 >>> m = processing.Manager()
1793 [INFO/SyncManager-1] child process calling self.run()
1794 [INFO/SyncManager-1] manager bound to '\\\\.\\pipe\\pyc-2776-0-lj0tfa'
1795 >>> del m
1796 [INFO/MainProcess] sending shutdown message to manager
1797 [INFO/SyncManager-1] manager exiting with exitcode 0
1798
1799
1800The :mod:`multiprocessing.dummy` module
1801~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1802
1803.. module:: multiprocessing.dummy
1804 :synopsis: Dumb wrapper around threading.
1805
1806:mod:`multiprocessing.dummy` replicates the API of :mod:`multiprocessing` but is
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001807no more than a wrapper around the :mod:`threading` module.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001808
1809
1810.. _multiprocessing-programming:
1811
1812Programming guidelines
1813----------------------
1814
1815There are certain guidelines and idioms which should be adhered to when using
1816:mod:`multiprocessing`.
1817
1818
1819All platforms
1820~~~~~~~~~~~~~
1821
1822Avoid shared state
1823
1824 As far as possible one should try to avoid shifting large amounts of data
1825 between processes.
1826
1827 It is probably best to stick to using queues or pipes for communication
1828 between processes rather than using the lower level synchronization
1829 primitives from the :mod:`threading` module.
1830
1831Picklability
1832
1833 Ensure that the arguments to the methods of proxies are picklable.
1834
1835Thread safety of proxies
1836
1837 Do not use a proxy object from more than one thread unless you protect it
1838 with a lock.
1839
1840 (There is never a problem with different processes using the *same* proxy.)
1841
1842Joining zombie processes
1843
1844 On Unix when a process finishes but has not been joined it becomes a zombie.
1845 There should never be very many because each time a new process starts (or
1846 :func:`active_children` is called) all completed processes which have not
1847 yet been joined will be joined. Also calling a finished process's
1848 :meth:`Process.is_alive` will join the process. Even so it is probably good
1849 practice to explicitly join all the processes that you start.
1850
1851Better to inherit than pickle/unpickle
1852
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001853 On Windows many types from :mod:`multiprocessing` need to be picklable so
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001854 that child processes can use them. However, one should generally avoid
1855 sending shared objects to other processes using pipes or queues. Instead
1856 you should arrange the program so that a process which need access to a
1857 shared resource created elsewhere can inherit it from an ancestor process.
1858
1859Avoid terminating processes
1860
1861 Using the :meth:`Process.terminate` method to stop a process is liable to
1862 cause any shared resources (such as locks, semaphores, pipes and queues)
1863 currently being used by the process to become broken or unavailable to other
1864 processes.
1865
1866 Therefore it is probably best to only consider using
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001867 :meth:`Process.terminate` on processes which never use any shared resources.
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001868
1869Joining processes that use queues
1870
1871 Bear in mind that a process that has put items in a queue will wait before
1872 terminating until all the buffered items are fed by the "feeder" thread to
1873 the underlying pipe. (The child process can call the
1874 :meth:`Queue.cancel_join` method of the queue to avoid this behaviour.)
1875
1876 This means that whenever you use a queue you need to make sure that all
1877 items which have been put on the queue will eventually be removed before the
1878 process is joined. Otherwise you cannot be sure that processes which have
1879 put items on the queue will terminate. Remember also that non-daemonic
1880 processes will be automatically be joined.
1881
1882 An example which will deadlock is the following::
1883
1884 from multiprocessing import Process, Queue
1885
1886 def f(q):
1887 q.put('X' * 1000000)
1888
1889 if __name__ == '__main__':
1890 queue = Queue()
1891 p = Process(target=f, args=(queue,))
1892 p.start()
1893 p.join() # this deadlocks
1894 obj = queue.get()
1895
1896 A fix here would be to swap the last two lines round (or simply remove the
1897 ``p.join()`` line).
1898
Andrew M. Kuchlingbe504f12008-06-19 19:48:42 +00001899Explicitly pass resources to child processes
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001900
1901 On Unix a child process can make use of a shared resource created in a
1902 parent process using a global resource. However, it is better to pass the
1903 object as an argument to the constructor for the child process.
1904
1905 Apart from making the code (potentially) compatible with Windows this also
1906 ensures that as long as the child process is still alive the object will not
1907 be garbage collected in the parent process. This might be important if some
1908 resource is freed when the object is garbage collected in the parent
1909 process.
1910
1911 So for instance ::
1912
1913 from multiprocessing import Process, Lock
1914
1915 def f():
1916 ... do something using "lock" ...
1917
1918 if __name__ == '__main__':
1919 lock = Lock()
1920 for i in range(10):
1921 Process(target=f).start()
1922
1923 should be rewritten as ::
1924
1925 from multiprocessing import Process, Lock
1926
1927 def f(l):
1928 ... do something using "l" ...
1929
1930 if __name__ == '__main__':
1931 lock = Lock()
1932 for i in range(10):
1933 Process(target=f, args=(lock,)).start()
1934
1935
1936Windows
1937~~~~~~~
1938
1939Since Windows lacks :func:`os.fork` it has a few extra restrictions:
1940
1941More picklability
1942
1943 Ensure that all arguments to :meth:`Process.__init__` are picklable. This
1944 means, in particular, that bound or unbound methods cannot be used directly
1945 as the ``target`` argument on Windows --- just define a function and use
1946 that instead.
1947
1948 Also, if you subclass :class:`Process` then make sure that instances will be
1949 picklable when the :meth:`Process.start` method is called.
1950
1951Global variables
1952
1953 Bear in mind that if code run in a child process tries to access a global
1954 variable, then the value it sees (if any) may not be the same as the value
1955 in the parent process at the time that :meth:`Process.start` was called.
1956
1957 However, global variables which are just module level constants cause no
1958 problems.
1959
1960Safe importing of main module
1961
1962 Make sure that the main module can be safely imported by a new Python
1963 interpreter without causing unintended side effects (such a starting a new
1964 process).
1965
1966 For example, under Windows running the following module would fail with a
1967 :exc:`RuntimeError`::
1968
1969 from multiprocessing import Process
1970
1971 def foo():
1972 print 'hello'
1973
1974 p = Process(target=foo)
1975 p.start()
1976
1977 Instead one should protect the "entry point" of the program by using ``if
1978 __name__ == '__main__':`` as follows::
1979
1980 from multiprocessing import Process, freeze_support
1981
1982 def foo():
1983 print 'hello'
1984
1985 if __name__ == '__main__':
1986 freeze_support()
1987 p = Process(target=foo)
1988 p.start()
1989
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00001990 (The ``freeze_support()`` line can be omitted if the program will be run
Benjamin Peterson190d56e2008-06-11 02:40:25 +00001991 normally instead of frozen.)
1992
1993 This allows the newly spawned Python interpreter to safely import the module
1994 and then run the module's ``foo()`` function.
1995
1996 Similar restrictions apply if a pool or manager is created in the main
1997 module.
1998
1999
2000.. _multiprocessing-examples:
2001
2002Examples
2003--------
2004
2005Demonstration of how to create and use customized managers and proxies:
2006
2007.. literalinclude:: ../includes/mp_newtype.py
2008
2009
2010Using :class:`Pool`:
2011
2012.. literalinclude:: ../includes/mp_pool.py
2013
2014
2015Synchronization types like locks, conditions and queues:
2016
2017.. literalinclude:: ../includes/mp_synchronize.py
2018
2019
2020An showing how to use queues to feed tasks to a collection of worker process and
2021collect the results:
2022
2023.. literalinclude:: ../includes/mp_workers.py
2024
2025
2026An example of how a pool of worker processes can each run a
2027:class:`SimpleHTTPServer.HttpServer` instance while sharing a single listening
2028socket.
2029
2030.. literalinclude:: ../includes/mp_webserver.py
2031
2032
2033Some simple benchmarks comparing :mod:`multiprocessing` with :mod:`threading`:
2034
2035.. literalinclude:: ../includes/mp_benchmarks.py
2036
2037An example/demo of how to use the :class:`managers.SyncManager`, :class:`Process`
2038and others to build a system which can distribute processes and work via a
2039distributed queue to a "cluster" of machines on a network, accessible via SSH.
2040You will need to have private key authentication for all hosts configured for
2041this to work.
2042
Benjamin Peterson910c2ab2008-06-27 23:22:06 +00002043.. literalinclude:: ../includes/mp_distributing.py