blob: 0b4634934291c397e109dd68285442b46b5c5286 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`threading` --- Higher-level threading interface
2=====================================================
3
4.. module:: threading
5 :synopsis: Higher-level threading interface.
6
7
8This module constructs higher-level threading interfaces on top of the lower
9level :mod:`thread` module.
Georg Brandla6168f92008-05-25 07:20:14 +000010See also the :mod:`mutex` and :mod:`Queue` modules.
Georg Brandl8ec7f652007-08-15 14:28:01 +000011
12The :mod:`dummy_threading` module is provided for situations where
13:mod:`threading` cannot be used because :mod:`thread` is missing.
14
Benjamin Petersonf4395602008-06-11 17:50:00 +000015.. note::
16
Victor Stinner8ded4772010-05-14 14:20:07 +000017 Starting with Python 2.6, this module provides :pep:`8` compliant aliases and
Benjamin Peterson973e6c22008-09-01 23:12:58 +000018 properties to replace the ``camelCase`` names that were inspired by Java's
19 threading API. This updated API is compatible with that of the
20 :mod:`multiprocessing` module. However, no schedule has been set for the
21 deprecation of the ``camelCase`` names and they remain fully supported in
22 both Python 2.x and 3.x.
Benjamin Petersonf4395602008-06-11 17:50:00 +000023
Georg Brandl2cd82a82009-03-09 14:25:07 +000024.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
Georg Brandl2cd82a82009-03-09 14:25:07 +000026 Starting with Python 2.5, several Thread methods raise :exc:`RuntimeError`
27 instead of :exc:`AssertionError` if called erroneously.
28
Antoine Pitrou9f41bb32011-01-06 16:35:14 +000029.. impl-detail::
30
31 Due to the :term:`Global Interpreter Lock`, in CPython only one thread
32 can execute Python code at once (even though certain performance-oriented
33 libraries might overcome this limitation).
34 If you want your application to make better of use of the computational
35 resources of multi-core machines, you are advised to use
36 :mod:`multiprocessing`. However, threading is still an appropriate model
37 if you want to run multiple I/O-bound tasks simultaneously.
38
Raymond Hettingere679a372010-11-05 23:58:42 +000039.. seealso::
40
41 Latest version of the `threading module Python source code
42 <http://svn.python.org/view/python/branches/release27-maint/Lib/threading.py?view=markup>`_
Georg Brandl2cd82a82009-03-09 14:25:07 +000043
Antoine Pitrou9f41bb32011-01-06 16:35:14 +000044
Georg Brandl2cd82a82009-03-09 14:25:07 +000045This module defines the following functions and objects:
Georg Brandl8ec7f652007-08-15 14:28:01 +000046
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000047.. function:: active_count()
Benjamin Petersonf4395602008-06-11 17:50:00 +000048 activeCount()
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
50 Return the number of :class:`Thread` objects currently alive. The returned
Georg Brandlf4da6662009-09-19 12:04:16 +000051 count is equal to the length of the list returned by :func:`.enumerate`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000052
53
54.. function:: Condition()
55 :noindex:
56
57 A factory function that returns a new condition variable object. A condition
58 variable allows one or more threads to wait until they are notified by another
59 thread.
60
Georg Brandl21946af2010-10-06 09:28:45 +000061 See :ref:`condition-objects`.
62
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000064.. function:: current_thread()
Benjamin Petersonf4395602008-06-11 17:50:00 +000065 currentThread()
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
67 Return the current :class:`Thread` object, corresponding to the caller's thread
68 of control. If the caller's thread of control was not created through the
69 :mod:`threading` module, a dummy thread object with limited functionality is
70 returned.
71
72
73.. function:: enumerate()
74
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000075 Return a list of all :class:`Thread` objects currently alive. The list
76 includes daemonic threads, dummy thread objects created by
77 :func:`current_thread`, and the main thread. It excludes terminated threads
78 and threads that have not yet been started.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
81.. function:: Event()
82 :noindex:
83
84 A factory function that returns a new event object. An event manages a flag
Georg Brandl9fa61bb2009-07-26 14:19:57 +000085 that can be set to true with the :meth:`~Event.set` method and reset to false
86 with the :meth:`clear` method. The :meth:`wait` method blocks until the flag
87 is true.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
Georg Brandl21946af2010-10-06 09:28:45 +000089 See :ref:`event-objects`.
90
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
92.. class:: local
93
94 A class that represents thread-local data. Thread-local data are data whose
95 values are thread specific. To manage thread-local data, just create an
96 instance of :class:`local` (or a subclass) and store attributes on it::
97
98 mydata = threading.local()
99 mydata.x = 1
100
101 The instance's values will be different for separate threads.
102
103 For more details and extensive examples, see the documentation string of the
104 :mod:`_threading_local` module.
105
106 .. versionadded:: 2.4
107
108
109.. function:: Lock()
110
111 A factory function that returns a new primitive lock object. Once a thread has
112 acquired it, subsequent attempts to acquire it block, until it is released; any
113 thread may release it.
114
Georg Brandl21946af2010-10-06 09:28:45 +0000115 See :ref:`lock-objects`.
116
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118.. function:: RLock()
119
120 A factory function that returns a new reentrant lock object. A reentrant lock
121 must be released by the thread that acquired it. Once a thread has acquired a
122 reentrant lock, the same thread may acquire it again without blocking; the
123 thread must release it once for each time it has acquired it.
124
Georg Brandl21946af2010-10-06 09:28:45 +0000125 See :ref:`rlock-objects`.
126
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128.. function:: Semaphore([value])
129 :noindex:
130
131 A factory function that returns a new semaphore object. A semaphore manages a
132 counter representing the number of :meth:`release` calls minus the number of
133 :meth:`acquire` calls, plus an initial value. The :meth:`acquire` method blocks
134 if necessary until it can return without making the counter negative. If not
135 given, *value* defaults to 1.
136
Georg Brandl21946af2010-10-06 09:28:45 +0000137 See :ref:`semaphore-objects`.
138
Georg Brandl8ec7f652007-08-15 14:28:01 +0000139
140.. function:: BoundedSemaphore([value])
141
142 A factory function that returns a new bounded semaphore object. A bounded
143 semaphore checks to make sure its current value doesn't exceed its initial
144 value. If it does, :exc:`ValueError` is raised. In most situations semaphores
145 are used to guard resources with limited capacity. If the semaphore is released
146 too many times it's a sign of a bug. If not given, *value* defaults to 1.
147
148
149.. class:: Thread
Georg Brandl21946af2010-10-06 09:28:45 +0000150 :noindex:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
152 A class that represents a thread of control. This class can be safely
153 subclassed in a limited fashion.
154
Georg Brandl21946af2010-10-06 09:28:45 +0000155 See :ref:`thread-objects`.
156
Georg Brandl8ec7f652007-08-15 14:28:01 +0000157
158.. class:: Timer
Georg Brandl21946af2010-10-06 09:28:45 +0000159 :noindex:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
161 A thread that executes a function after a specified interval has passed.
162
Georg Brandl21946af2010-10-06 09:28:45 +0000163 See :ref:`timer-objects`.
164
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
166.. function:: settrace(func)
167
168 .. index:: single: trace function
169
170 Set a trace function for all threads started from the :mod:`threading` module.
171 The *func* will be passed to :func:`sys.settrace` for each thread, before its
172 :meth:`run` method is called.
173
174 .. versionadded:: 2.3
175
176
177.. function:: setprofile(func)
178
179 .. index:: single: profile function
180
181 Set a profile function for all threads started from the :mod:`threading` module.
182 The *func* will be passed to :func:`sys.setprofile` for each thread, before its
183 :meth:`run` method is called.
184
185 .. versionadded:: 2.3
186
187
188.. function:: stack_size([size])
189
190 Return the thread stack size used when creating new threads. The optional
191 *size* argument specifies the stack size to be used for subsequently created
192 threads, and must be 0 (use platform or configured default) or a positive
193 integer value of at least 32,768 (32kB). If changing the thread stack size is
194 unsupported, a :exc:`ThreadError` is raised. If the specified stack size is
195 invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32kB
196 is currently the minimum supported stack size value to guarantee sufficient
197 stack space for the interpreter itself. Note that some platforms may have
198 particular restrictions on values for the stack size, such as requiring a
199 minimum stack size > 32kB or requiring allocation in multiples of the system
200 memory page size - platform documentation should be referred to for more
201 information (4kB pages are common; using multiples of 4096 for the stack size is
202 the suggested approach in the absence of more specific information).
203 Availability: Windows, systems with POSIX threads.
204
205 .. versionadded:: 2.5
206
207Detailed interfaces for the objects are documented below.
208
209The design of this module is loosely based on Java's threading model. However,
210where Java makes locks and condition variables basic behavior of every object,
211they are separate objects in Python. Python's :class:`Thread` class supports a
212subset of the behavior of Java's Thread class; currently, there are no
213priorities, no thread groups, and threads cannot be destroyed, stopped,
214suspended, resumed, or interrupted. The static methods of Java's Thread class,
215when implemented, are mapped to module-level functions.
216
217All of the methods described below are executed atomically.
218
219
Georg Brandl01ba86a2008-11-06 10:20:49 +0000220.. _thread-objects:
221
222Thread Objects
223--------------
224
225This class represents an activity that is run in a separate thread of control.
226There are two ways to specify the activity: by passing a callable object to the
227constructor, or by overriding the :meth:`run` method in a subclass. No other
228methods (except for the constructor) should be overridden in a subclass. In
229other words, *only* override the :meth:`__init__` and :meth:`run` methods of
230this class.
231
232Once a thread object is created, its activity must be started by calling the
233thread's :meth:`start` method. This invokes the :meth:`run` method in a
234separate thread of control.
235
236Once the thread's activity is started, the thread is considered 'alive'. It
237stops being alive when its :meth:`run` method terminates -- either normally, or
238by raising an unhandled exception. The :meth:`is_alive` method tests whether the
239thread is alive.
240
241Other threads can call a thread's :meth:`join` method. This blocks the calling
242thread until the thread whose :meth:`join` method is called is terminated.
243
244A thread has a name. The name can be passed to the constructor, and read or
245changed through the :attr:`name` attribute.
246
247A thread can be flagged as a "daemon thread". The significance of this flag is
248that the entire Python program exits when only daemon threads are left. The
249initial value is inherited from the creating thread. The flag can be set
Georg Brandlecd2afa2009-02-05 11:40:35 +0000250through the :attr:`daemon` property.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000251
252There is a "main thread" object; this corresponds to the initial thread of
253control in the Python program. It is not a daemon thread.
254
255There is the possibility that "dummy thread objects" are created. These are
256thread objects corresponding to "alien threads", which are threads of control
257started outside the threading module, such as directly from C code. Dummy
258thread objects have limited functionality; they are always considered alive and
259daemonic, and cannot be :meth:`join`\ ed. They are never deleted, since it is
260impossible to detect the termination of alien threads.
261
262
263.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={})
264
Georg Brandl3591a8f2009-07-26 14:44:23 +0000265 This constructor should always be called with keyword arguments. Arguments
266 are:
Georg Brandl01ba86a2008-11-06 10:20:49 +0000267
268 *group* should be ``None``; reserved for future extension when a
269 :class:`ThreadGroup` class is implemented.
270
271 *target* is the callable object to be invoked by the :meth:`run` method.
272 Defaults to ``None``, meaning nothing is called.
273
Georg Brandl3591a8f2009-07-26 14:44:23 +0000274 *name* is the thread name. By default, a unique name is constructed of the
275 form "Thread-*N*" where *N* is a small decimal number.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000276
277 *args* is the argument tuple for the target invocation. Defaults to ``()``.
278
279 *kwargs* is a dictionary of keyword arguments for the target invocation.
280 Defaults to ``{}``.
281
Georg Brandl3591a8f2009-07-26 14:44:23 +0000282 If the subclass overrides the constructor, it must make sure to invoke the
283 base class constructor (``Thread.__init__()``) before doing anything else to
284 the thread.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000285
Georg Brandl3591a8f2009-07-26 14:44:23 +0000286 .. method:: start()
Georg Brandl01ba86a2008-11-06 10:20:49 +0000287
Georg Brandl3591a8f2009-07-26 14:44:23 +0000288 Start the thread's activity.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000289
Georg Brandl3591a8f2009-07-26 14:44:23 +0000290 It must be called at most once per thread object. It arranges for the
291 object's :meth:`run` method to be invoked in a separate thread of control.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000292
Brian Curtin37c4a722011-01-31 19:55:14 +0000293 This method will raise a :exc:`RuntimeError` if called more than once
Georg Brandl3591a8f2009-07-26 14:44:23 +0000294 on the same thread object.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000295
Georg Brandl3591a8f2009-07-26 14:44:23 +0000296 .. method:: run()
Georg Brandl01ba86a2008-11-06 10:20:49 +0000297
Georg Brandl3591a8f2009-07-26 14:44:23 +0000298 Method representing the thread's activity.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000299
Georg Brandl3591a8f2009-07-26 14:44:23 +0000300 You may override this method in a subclass. The standard :meth:`run`
301 method invokes the callable object passed to the object's constructor as
302 the *target* argument, if any, with sequential and keyword arguments taken
303 from the *args* and *kwargs* arguments, respectively.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000304
Georg Brandl3591a8f2009-07-26 14:44:23 +0000305 .. method:: join([timeout])
Georg Brandl01ba86a2008-11-06 10:20:49 +0000306
Georg Brandl3591a8f2009-07-26 14:44:23 +0000307 Wait until the thread terminates. This blocks the calling thread until the
308 thread whose :meth:`join` method is called terminates -- either normally
309 or through an unhandled exception -- or until the optional timeout occurs.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000310
Georg Brandl3591a8f2009-07-26 14:44:23 +0000311 When the *timeout* argument is present and not ``None``, it should be a
312 floating point number specifying a timeout for the operation in seconds
313 (or fractions thereof). As :meth:`join` always returns ``None``, you must
314 call :meth:`isAlive` after :meth:`join` to decide whether a timeout
315 happened -- if the thread is still alive, the :meth:`join` call timed out.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000316
Georg Brandl3591a8f2009-07-26 14:44:23 +0000317 When the *timeout* argument is not present or ``None``, the operation will
318 block until the thread terminates.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000319
Georg Brandl3591a8f2009-07-26 14:44:23 +0000320 A thread can be :meth:`join`\ ed many times.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000321
Georg Brandl3591a8f2009-07-26 14:44:23 +0000322 :meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
323 the current thread as that would cause a deadlock. It is also an error to
324 :meth:`join` a thread before it has been started and attempts to do so
325 raises the same exception.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000326
Georg Brandl3591a8f2009-07-26 14:44:23 +0000327 .. method:: getName()
328 setName()
Georg Brandl01ba86a2008-11-06 10:20:49 +0000329
Georg Brandl3591a8f2009-07-26 14:44:23 +0000330 Old API for :attr:`~Thread.name`.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000331
Georg Brandl3591a8f2009-07-26 14:44:23 +0000332 .. attribute:: name
Georg Brandl01ba86a2008-11-06 10:20:49 +0000333
Georg Brandl3591a8f2009-07-26 14:44:23 +0000334 A string used for identification purposes only. It has no semantics.
335 Multiple threads may be given the same name. The initial name is set by
336 the constructor.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000337
Georg Brandl3591a8f2009-07-26 14:44:23 +0000338 .. attribute:: ident
Georg Brandl01ba86a2008-11-06 10:20:49 +0000339
Georg Brandl3591a8f2009-07-26 14:44:23 +0000340 The 'thread identifier' of this thread or ``None`` if the thread has not
341 been started. This is a nonzero integer. See the
342 :func:`thread.get_ident()` function. Thread identifiers may be recycled
343 when a thread exits and another thread is created. The identifier is
344 available even after the thread has exited.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000345
Georg Brandl3591a8f2009-07-26 14:44:23 +0000346 .. versionadded:: 2.6
Georg Brandl01ba86a2008-11-06 10:20:49 +0000347
Georg Brandl3591a8f2009-07-26 14:44:23 +0000348 .. method:: is_alive()
349 isAlive()
Georg Brandl01ba86a2008-11-06 10:20:49 +0000350
Georg Brandl3591a8f2009-07-26 14:44:23 +0000351 Return whether the thread is alive.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000352
Brett Cannon11a30612010-07-23 12:30:10 +0000353 This method returns ``True`` just before the :meth:`run` method starts
354 until just after the :meth:`run` method terminates. The module function
Georg Brandlf4da6662009-09-19 12:04:16 +0000355 :func:`.enumerate` returns a list of all alive threads.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000356
Georg Brandl3591a8f2009-07-26 14:44:23 +0000357 .. method:: isDaemon()
358 setDaemon()
Georg Brandl01ba86a2008-11-06 10:20:49 +0000359
Georg Brandl3591a8f2009-07-26 14:44:23 +0000360 Old API for :attr:`~Thread.daemon`.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000361
Georg Brandl3591a8f2009-07-26 14:44:23 +0000362 .. attribute:: daemon
Georg Brandl01ba86a2008-11-06 10:20:49 +0000363
Georg Brandl3591a8f2009-07-26 14:44:23 +0000364 A boolean value indicating whether this thread is a daemon thread (True)
365 or not (False). This must be set before :meth:`start` is called,
366 otherwise :exc:`RuntimeError` is raised. Its initial value is inherited
367 from the creating thread; the main thread is not a daemon thread and
368 therefore all threads created in the main thread default to :attr:`daemon`
369 = ``False``.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000370
Georg Brandl3591a8f2009-07-26 14:44:23 +0000371 The entire Python program exits when no alive non-daemon threads are left.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000372
373
Georg Brandl8ec7f652007-08-15 14:28:01 +0000374.. _lock-objects:
375
376Lock Objects
377------------
378
379A primitive lock is a synchronization primitive that is not owned by a
380particular thread when locked. In Python, it is currently the lowest level
381synchronization primitive available, implemented directly by the :mod:`thread`
382extension module.
383
384A primitive lock is in one of two states, "locked" or "unlocked". It is created
385in the unlocked state. It has two basic methods, :meth:`acquire` and
386:meth:`release`. When the state is unlocked, :meth:`acquire` changes the state
387to locked and returns immediately. When the state is locked, :meth:`acquire`
388blocks until a call to :meth:`release` in another thread changes it to unlocked,
389then the :meth:`acquire` call resets it to locked and returns. The
390:meth:`release` method should only be called in the locked state; it changes the
391state to unlocked and returns immediately. If an attempt is made to release an
392unlocked lock, a :exc:`RuntimeError` will be raised.
393
394When more than one thread is blocked in :meth:`acquire` waiting for the state to
395turn to unlocked, only one thread proceeds when a :meth:`release` call resets
396the state to unlocked; which one of the waiting threads proceeds is not defined,
397and may vary across implementations.
398
399All methods are executed atomically.
400
401
Terry Reedyc0b35442011-01-01 00:36:18 +0000402.. method:: Lock.acquire([blocking])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000403
404 Acquire a lock, blocking or non-blocking.
405
406 When invoked without arguments, block until the lock is unlocked, then set it to
407 locked, and return true.
408
409 When invoked with the *blocking* argument set to true, do the same thing as when
410 called without arguments, and return true.
411
412 When invoked with the *blocking* argument set to false, do not block. If a call
413 without an argument would block, return false immediately; otherwise, do the
414 same thing as when called without arguments, and return true.
415
416
417.. method:: Lock.release()
418
419 Release a lock.
420
421 When the lock is locked, reset it to unlocked, and return. If any other threads
422 are blocked waiting for the lock to become unlocked, allow exactly one of them
423 to proceed.
424
425 Do not call this method when the lock is unlocked.
426
427 There is no return value.
428
429
430.. _rlock-objects:
431
432RLock Objects
433-------------
434
435A reentrant lock is a synchronization primitive that may be acquired multiple
436times by the same thread. Internally, it uses the concepts of "owning thread"
437and "recursion level" in addition to the locked/unlocked state used by primitive
438locks. In the locked state, some thread owns the lock; in the unlocked state,
439no thread owns it.
440
441To lock the lock, a thread calls its :meth:`acquire` method; this returns once
442the thread owns the lock. To unlock the lock, a thread calls its
443:meth:`release` method. :meth:`acquire`/:meth:`release` call pairs may be
444nested; only the final :meth:`release` (the :meth:`release` of the outermost
445pair) resets the lock to unlocked and allows another thread blocked in
446:meth:`acquire` to proceed.
447
448
449.. method:: RLock.acquire([blocking=1])
450
451 Acquire a lock, blocking or non-blocking.
452
453 When invoked without arguments: if this thread already owns the lock, increment
454 the recursion level by one, and return immediately. Otherwise, if another
455 thread owns the lock, block until the lock is unlocked. Once the lock is
456 unlocked (not owned by any thread), then grab ownership, set the recursion level
457 to one, and return. If more than one thread is blocked waiting until the lock
458 is unlocked, only one at a time will be able to grab ownership of the lock.
459 There is no return value in this case.
460
461 When invoked with the *blocking* argument set to true, do the same thing as when
462 called without arguments, and return true.
463
464 When invoked with the *blocking* argument set to false, do not block. If a call
465 without an argument would block, return false immediately; otherwise, do the
466 same thing as when called without arguments, and return true.
467
468
469.. method:: RLock.release()
470
471 Release a lock, decrementing the recursion level. If after the decrement it is
472 zero, reset the lock to unlocked (not owned by any thread), and if any other
473 threads are blocked waiting for the lock to become unlocked, allow exactly one
474 of them to proceed. If after the decrement the recursion level is still
475 nonzero, the lock remains locked and owned by the calling thread.
476
477 Only call this method when the calling thread owns the lock. A
478 :exc:`RuntimeError` is raised if this method is called when the lock is
479 unlocked.
480
481 There is no return value.
482
483
484.. _condition-objects:
485
486Condition Objects
487-----------------
488
489A condition variable is always associated with some kind of lock; this can be
490passed in or one will be created by default. (Passing one in is useful when
491several condition variables must share the same lock.)
492
493A condition variable has :meth:`acquire` and :meth:`release` methods that call
494the corresponding methods of the associated lock. It also has a :meth:`wait`
495method, and :meth:`notify` and :meth:`notifyAll` methods. These three must only
496be called when the calling thread has acquired the lock, otherwise a
497:exc:`RuntimeError` is raised.
498
499The :meth:`wait` method releases the lock, and then blocks until it is awakened
500by a :meth:`notify` or :meth:`notifyAll` call for the same condition variable in
501another thread. Once awakened, it re-acquires the lock and returns. It is also
502possible to specify a timeout.
503
504The :meth:`notify` method wakes up one of the threads waiting for the condition
505variable, if any are waiting. The :meth:`notifyAll` method wakes up all threads
506waiting for the condition variable.
507
508Note: the :meth:`notify` and :meth:`notifyAll` methods don't release the lock;
509this means that the thread or threads awakened will not return from their
510:meth:`wait` call immediately, but only when the thread that called
511:meth:`notify` or :meth:`notifyAll` finally relinquishes ownership of the lock.
512
513Tip: the typical programming style using condition variables uses the lock to
514synchronize access to some shared state; threads that are interested in a
515particular change of state call :meth:`wait` repeatedly until they see the
516desired state, while threads that modify the state call :meth:`notify` or
517:meth:`notifyAll` when they change the state in such a way that it could
518possibly be a desired state for one of the waiters. For example, the following
519code is a generic producer-consumer situation with unlimited buffer capacity::
520
521 # Consume one item
522 cv.acquire()
523 while not an_item_is_available():
524 cv.wait()
525 get_an_available_item()
526 cv.release()
527
528 # Produce one item
529 cv.acquire()
530 make_an_item_available()
531 cv.notify()
532 cv.release()
533
534To choose between :meth:`notify` and :meth:`notifyAll`, consider whether one
535state change can be interesting for only one or several waiting threads. E.g.
536in a typical producer-consumer situation, adding one item to the buffer only
537needs to wake up one consumer thread.
538
539
540.. class:: Condition([lock])
541
Georg Brandl3591a8f2009-07-26 14:44:23 +0000542 If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
543 or :class:`RLock` object, and it is used as the underlying lock. Otherwise,
544 a new :class:`RLock` object is created and used as the underlying lock.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000545
Georg Brandl3591a8f2009-07-26 14:44:23 +0000546 .. method:: acquire(*args)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000547
Georg Brandl3591a8f2009-07-26 14:44:23 +0000548 Acquire the underlying lock. This method calls the corresponding method on
549 the underlying lock; the return value is whatever that method returns.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000550
Georg Brandl3591a8f2009-07-26 14:44:23 +0000551 .. method:: release()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000552
Georg Brandl3591a8f2009-07-26 14:44:23 +0000553 Release the underlying lock. This method calls the corresponding method on
554 the underlying lock; there is no return value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000555
Georg Brandl3591a8f2009-07-26 14:44:23 +0000556 .. method:: wait([timeout])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000557
Georg Brandl3591a8f2009-07-26 14:44:23 +0000558 Wait until notified or until a timeout occurs. If the calling thread has not
559 acquired the lock when this method is called, a :exc:`RuntimeError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000560
Georg Brandl3591a8f2009-07-26 14:44:23 +0000561 This method releases the underlying lock, and then blocks until it is
562 awakened by a :meth:`notify` or :meth:`notifyAll` call for the same
563 condition variable in another thread, or until the optional timeout
564 occurs. Once awakened or timed out, it re-acquires the lock and returns.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000565
Georg Brandl3591a8f2009-07-26 14:44:23 +0000566 When the *timeout* argument is present and not ``None``, it should be a
567 floating point number specifying a timeout for the operation in seconds
568 (or fractions thereof).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000569
Georg Brandl3591a8f2009-07-26 14:44:23 +0000570 When the underlying lock is an :class:`RLock`, it is not released using
571 its :meth:`release` method, since this may not actually unlock the lock
572 when it was acquired multiple times recursively. Instead, an internal
573 interface of the :class:`RLock` class is used, which really unlocks it
574 even when it has been recursively acquired several times. Another internal
575 interface is then used to restore the recursion level when the lock is
576 reacquired.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000577
Georg Brandl3591a8f2009-07-26 14:44:23 +0000578 .. method:: notify()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000579
Georg Brandl3591a8f2009-07-26 14:44:23 +0000580 Wake up a thread waiting on this condition, if any. If the calling thread
581 has not acquired the lock when this method is called, a
582 :exc:`RuntimeError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000583
Georg Brandl3591a8f2009-07-26 14:44:23 +0000584 This method wakes up one of the threads waiting for the condition
585 variable, if any are waiting; it is a no-op if no threads are waiting.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000586
Georg Brandl3591a8f2009-07-26 14:44:23 +0000587 The current implementation wakes up exactly one thread, if any are
588 waiting. However, it's not safe to rely on this behavior. A future,
589 optimized implementation may occasionally wake up more than one thread.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000590
Georg Brandl3591a8f2009-07-26 14:44:23 +0000591 Note: the awakened thread does not actually return from its :meth:`wait`
592 call until it can reacquire the lock. Since :meth:`notify` does not
593 release the lock, its caller should.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000594
Georg Brandl3591a8f2009-07-26 14:44:23 +0000595 .. method:: notify_all()
596 notifyAll()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000597
Georg Brandl3591a8f2009-07-26 14:44:23 +0000598 Wake up all threads waiting on this condition. This method acts like
599 :meth:`notify`, but wakes up all waiting threads instead of one. If the
600 calling thread has not acquired the lock when this method is called, a
601 :exc:`RuntimeError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000602
603
604.. _semaphore-objects:
605
606Semaphore Objects
607-----------------
608
609This is one of the oldest synchronization primitives in the history of computer
610science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he
611used :meth:`P` and :meth:`V` instead of :meth:`acquire` and :meth:`release`).
612
613A semaphore manages an internal counter which is decremented by each
614:meth:`acquire` call and incremented by each :meth:`release` call. The counter
615can never go below zero; when :meth:`acquire` finds that it is zero, it blocks,
616waiting until some other thread calls :meth:`release`.
617
618
619.. class:: Semaphore([value])
620
621 The optional argument gives the initial *value* for the internal counter; it
622 defaults to ``1``. If the *value* given is less than 0, :exc:`ValueError` is
623 raised.
624
Georg Brandl3591a8f2009-07-26 14:44:23 +0000625 .. method:: acquire([blocking])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000626
Georg Brandl3591a8f2009-07-26 14:44:23 +0000627 Acquire a semaphore.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000628
Georg Brandl3591a8f2009-07-26 14:44:23 +0000629 When invoked without arguments: if the internal counter is larger than
630 zero on entry, decrement it by one and return immediately. If it is zero
631 on entry, block, waiting until some other thread has called
632 :meth:`release` to make it larger than zero. This is done with proper
633 interlocking so that if multiple :meth:`acquire` calls are blocked,
634 :meth:`release` will wake exactly one of them up. The implementation may
635 pick one at random, so the order in which blocked threads are awakened
636 should not be relied on. There is no return value in this case.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000637
Georg Brandl3591a8f2009-07-26 14:44:23 +0000638 When invoked with *blocking* set to true, do the same thing as when called
639 without arguments, and return true.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000640
Georg Brandl3591a8f2009-07-26 14:44:23 +0000641 When invoked with *blocking* set to false, do not block. If a call
642 without an argument would block, return false immediately; otherwise, do
643 the same thing as when called without arguments, and return true.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
Georg Brandl3591a8f2009-07-26 14:44:23 +0000645 .. method:: release()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000646
Georg Brandl3591a8f2009-07-26 14:44:23 +0000647 Release a semaphore, incrementing the internal counter by one. When it
648 was zero on entry and another thread is waiting for it to become larger
649 than zero again, wake up that thread.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000650
651
652.. _semaphore-examples:
653
654:class:`Semaphore` Example
655^^^^^^^^^^^^^^^^^^^^^^^^^^
656
657Semaphores are often used to guard resources with limited capacity, for example,
Georg Brandl335d4f52011-01-09 07:58:45 +0000658a database server. In any situation where the size of the resource is fixed,
659you should use a bounded semaphore. Before spawning any worker threads, your
660main thread would initialize the semaphore::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000661
662 maxconnections = 5
663 ...
664 pool_sema = BoundedSemaphore(value=maxconnections)
665
666Once spawned, worker threads call the semaphore's acquire and release methods
667when they need to connect to the server::
668
669 pool_sema.acquire()
670 conn = connectdb()
671 ... use connection ...
672 conn.close()
673 pool_sema.release()
674
675The use of a bounded semaphore reduces the chance that a programming error which
676causes the semaphore to be released more than it's acquired will go undetected.
677
678
679.. _event-objects:
680
681Event Objects
682-------------
683
684This is one of the simplest mechanisms for communication between threads: one
685thread signals an event and other threads wait for it.
686
687An event object manages an internal flag that can be set to true with the
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000688:meth:`~Event.set` method and reset to false with the :meth:`clear` method. The
Georg Brandl8ec7f652007-08-15 14:28:01 +0000689:meth:`wait` method blocks until the flag is true.
690
691
692.. class:: Event()
693
694 The internal flag is initially false.
695
Georg Brandl3591a8f2009-07-26 14:44:23 +0000696 .. method:: is_set()
697 isSet()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000698
Georg Brandl3591a8f2009-07-26 14:44:23 +0000699 Return true if and only if the internal flag is true.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000700
Facundo Batista47b66592010-01-25 06:15:01 +0000701 .. versionchanged:: 2.6
702 The ``is_set()`` syntax is new.
703
Georg Brandl3591a8f2009-07-26 14:44:23 +0000704 .. method:: set()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000705
Georg Brandl3591a8f2009-07-26 14:44:23 +0000706 Set the internal flag to true. All threads waiting for it to become true
707 are awakened. Threads that call :meth:`wait` once the flag is true will
708 not block at all.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000709
Georg Brandl3591a8f2009-07-26 14:44:23 +0000710 .. method:: clear()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000711
Georg Brandl3591a8f2009-07-26 14:44:23 +0000712 Reset the internal flag to false. Subsequently, threads calling
713 :meth:`wait` will block until :meth:`.set` is called to set the internal
714 flag to true again.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000715
Georg Brandl3591a8f2009-07-26 14:44:23 +0000716 .. method:: wait([timeout])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000717
Georg Brandl3591a8f2009-07-26 14:44:23 +0000718 Block until the internal flag is true. If the internal flag is true on
719 entry, return immediately. Otherwise, block until another thread calls
720 :meth:`.set` to set the flag to true, or until the optional timeout
721 occurs.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000722
Georg Brandl3591a8f2009-07-26 14:44:23 +0000723 When the timeout argument is present and not ``None``, it should be a
724 floating point number specifying a timeout for the operation in seconds
725 (or fractions thereof).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000726
Georg Brandl3591a8f2009-07-26 14:44:23 +0000727 This method returns the internal flag on exit, so it will always return
728 ``True`` except if a timeout is given and the operation times out.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000729
Georg Brandl3591a8f2009-07-26 14:44:23 +0000730 .. versionchanged:: 2.7
731 Previously, the method always returned ``None``.
Georg Brandlef660e82009-03-31 20:41:08 +0000732
Georg Brandl8ec7f652007-08-15 14:28:01 +0000733
Georg Brandl8ec7f652007-08-15 14:28:01 +0000734.. _timer-objects:
735
736Timer Objects
737-------------
738
739This class represents an action that should be run only after a certain amount
740of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
741and as such also functions as an example of creating custom threads.
742
743Timers are started, as with threads, by calling their :meth:`start` method. The
744timer can be stopped (before its action has begun) by calling the :meth:`cancel`
745method. The interval the timer will wait before executing its action may not be
746exactly the same as the interval specified by the user.
747
748For example::
749
750 def hello():
751 print "hello, world"
752
753 t = Timer(30.0, hello)
754 t.start() # after 30 seconds, "hello, world" will be printed
755
756
757.. class:: Timer(interval, function, args=[], kwargs={})
758
759 Create a timer that will run *function* with arguments *args* and keyword
760 arguments *kwargs*, after *interval* seconds have passed.
761
Georg Brandl3591a8f2009-07-26 14:44:23 +0000762 .. method:: cancel()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000763
Georg Brandl3591a8f2009-07-26 14:44:23 +0000764 Stop the timer, and cancel the execution of the timer's action. This will
765 only work if the timer is still in its waiting stage.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000766
767
768.. _with-locks:
769
770Using locks, conditions, and semaphores in the :keyword:`with` statement
771------------------------------------------------------------------------
772
773All of the objects provided by this module that have :meth:`acquire` and
774:meth:`release` methods can be used as context managers for a :keyword:`with`
775statement. The :meth:`acquire` method will be called when the block is entered,
776and :meth:`release` will be called when the block is exited.
777
778Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
779:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
780:keyword:`with` statement context managers. For example::
781
Georg Brandl8ec7f652007-08-15 14:28:01 +0000782 import threading
783
784 some_rlock = threading.RLock()
785
786 with some_rlock:
787 print "some_rlock is locked while this executes"
788
Georg Brandl2e255512008-03-13 07:21:41 +0000789
790.. _threaded-imports:
791
792Importing in threaded code
793--------------------------
794
Georg Brandl837fbb02010-11-26 07:58:55 +0000795While the import machinery is thread-safe, there are two key restrictions on
796threaded imports due to inherent limitations in the way that thread-safety is
797provided:
Georg Brandl2e255512008-03-13 07:21:41 +0000798
799* Firstly, other than in the main module, an import should not have the
800 side effect of spawning a new thread and then waiting for that thread in
801 any way. Failing to abide by this restriction can lead to a deadlock if
802 the spawned thread directly or indirectly attempts to import a module.
803* Secondly, all import attempts must be completed before the interpreter
804 starts shutting itself down. This can be most easily achieved by only
805 performing imports from non-daemon threads created through the threading
806 module. Daemon threads and threads created directly with the thread
807 module will require some other form of synchronization to ensure they do
808 not attempt imports after system shutdown has commenced. Failure to
809 abide by this restriction will lead to intermittent exceptions and
810 crashes during interpreter shutdown (as the late imports attempt to
811 access machinery which is no longer in a valid state).