blob: efd0cff7c34ae4b3a4b325fc00e97f83bf326508 [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
Benjamin Peterson973e6c22008-09-01 23:12:58 +000017 Starting with Python 2.6, this module provides PEP 8 compliant aliases and
18 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
29
30This module defines the following functions and objects:
Georg Brandl8ec7f652007-08-15 14:28:01 +000031
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000032.. function:: active_count()
Benjamin Petersonf4395602008-06-11 17:50:00 +000033 activeCount()
Georg Brandl8ec7f652007-08-15 14:28:01 +000034
35 Return the number of :class:`Thread` objects currently alive. The returned
36 count is equal to the length of the list returned by :func:`enumerate`.
37
38
39.. function:: Condition()
40 :noindex:
41
42 A factory function that returns a new condition variable object. A condition
43 variable allows one or more threads to wait until they are notified by another
44 thread.
45
46
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000047.. function:: current_thread()
Benjamin Petersonf4395602008-06-11 17:50:00 +000048 currentThread()
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
50 Return the current :class:`Thread` object, corresponding to the caller's thread
51 of control. If the caller's thread of control was not created through the
52 :mod:`threading` module, a dummy thread object with limited functionality is
53 returned.
54
55
56.. function:: enumerate()
57
Benjamin Peterson0fbcf692008-06-11 17:27:50 +000058 Return a list of all :class:`Thread` objects currently alive. The list
59 includes daemonic threads, dummy thread objects created by
60 :func:`current_thread`, and the main thread. It excludes terminated threads
61 and threads that have not yet been started.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63
64.. function:: Event()
65 :noindex:
66
67 A factory function that returns a new event object. An event manages a flag
68 that can be set to true with the :meth:`set` method and reset to false with the
69 :meth:`clear` method. The :meth:`wait` method blocks until the flag is true.
70
71
72.. class:: local
73
74 A class that represents thread-local data. Thread-local data are data whose
75 values are thread specific. To manage thread-local data, just create an
76 instance of :class:`local` (or a subclass) and store attributes on it::
77
78 mydata = threading.local()
79 mydata.x = 1
80
81 The instance's values will be different for separate threads.
82
83 For more details and extensive examples, see the documentation string of the
84 :mod:`_threading_local` module.
85
86 .. versionadded:: 2.4
87
88
89.. function:: Lock()
90
91 A factory function that returns a new primitive lock object. Once a thread has
92 acquired it, subsequent attempts to acquire it block, until it is released; any
93 thread may release it.
94
95
96.. function:: RLock()
97
98 A factory function that returns a new reentrant lock object. A reentrant lock
99 must be released by the thread that acquired it. Once a thread has acquired a
100 reentrant lock, the same thread may acquire it again without blocking; the
101 thread must release it once for each time it has acquired it.
102
103
104.. function:: Semaphore([value])
105 :noindex:
106
107 A factory function that returns a new semaphore object. A semaphore manages a
108 counter representing the number of :meth:`release` calls minus the number of
109 :meth:`acquire` calls, plus an initial value. The :meth:`acquire` method blocks
110 if necessary until it can return without making the counter negative. If not
111 given, *value* defaults to 1.
112
113
114.. function:: BoundedSemaphore([value])
115
116 A factory function that returns a new bounded semaphore object. A bounded
117 semaphore checks to make sure its current value doesn't exceed its initial
118 value. If it does, :exc:`ValueError` is raised. In most situations semaphores
119 are used to guard resources with limited capacity. If the semaphore is released
120 too many times it's a sign of a bug. If not given, *value* defaults to 1.
121
122
123.. class:: Thread
124
125 A class that represents a thread of control. This class can be safely
126 subclassed in a limited fashion.
127
128
129.. class:: Timer
130
131 A thread that executes a function after a specified interval has passed.
132
133
134.. function:: settrace(func)
135
136 .. index:: single: trace function
137
138 Set a trace function for all threads started from the :mod:`threading` module.
139 The *func* will be passed to :func:`sys.settrace` for each thread, before its
140 :meth:`run` method is called.
141
142 .. versionadded:: 2.3
143
144
145.. function:: setprofile(func)
146
147 .. index:: single: profile function
148
149 Set a profile function for all threads started from the :mod:`threading` module.
150 The *func* will be passed to :func:`sys.setprofile` for each thread, before its
151 :meth:`run` method is called.
152
153 .. versionadded:: 2.3
154
155
156.. function:: stack_size([size])
157
158 Return the thread stack size used when creating new threads. The optional
159 *size* argument specifies the stack size to be used for subsequently created
160 threads, and must be 0 (use platform or configured default) or a positive
161 integer value of at least 32,768 (32kB). If changing the thread stack size is
162 unsupported, a :exc:`ThreadError` is raised. If the specified stack size is
163 invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32kB
164 is currently the minimum supported stack size value to guarantee sufficient
165 stack space for the interpreter itself. Note that some platforms may have
166 particular restrictions on values for the stack size, such as requiring a
167 minimum stack size > 32kB or requiring allocation in multiples of the system
168 memory page size - platform documentation should be referred to for more
169 information (4kB pages are common; using multiples of 4096 for the stack size is
170 the suggested approach in the absence of more specific information).
171 Availability: Windows, systems with POSIX threads.
172
173 .. versionadded:: 2.5
174
175Detailed interfaces for the objects are documented below.
176
177The design of this module is loosely based on Java's threading model. However,
178where Java makes locks and condition variables basic behavior of every object,
179they are separate objects in Python. Python's :class:`Thread` class supports a
180subset of the behavior of Java's Thread class; currently, there are no
181priorities, no thread groups, and threads cannot be destroyed, stopped,
182suspended, resumed, or interrupted. The static methods of Java's Thread class,
183when implemented, are mapped to module-level functions.
184
185All of the methods described below are executed atomically.
186
187
Georg Brandl01ba86a2008-11-06 10:20:49 +0000188.. _thread-objects:
189
190Thread Objects
191--------------
192
193This class represents an activity that is run in a separate thread of control.
194There are two ways to specify the activity: by passing a callable object to the
195constructor, or by overriding the :meth:`run` method in a subclass. No other
196methods (except for the constructor) should be overridden in a subclass. In
197other words, *only* override the :meth:`__init__` and :meth:`run` methods of
198this class.
199
200Once a thread object is created, its activity must be started by calling the
201thread's :meth:`start` method. This invokes the :meth:`run` method in a
202separate thread of control.
203
204Once the thread's activity is started, the thread is considered 'alive'. It
205stops being alive when its :meth:`run` method terminates -- either normally, or
206by raising an unhandled exception. The :meth:`is_alive` method tests whether the
207thread is alive.
208
209Other threads can call a thread's :meth:`join` method. This blocks the calling
210thread until the thread whose :meth:`join` method is called is terminated.
211
212A thread has a name. The name can be passed to the constructor, and read or
213changed through the :attr:`name` attribute.
214
215A thread can be flagged as a "daemon thread". The significance of this flag is
216that the entire Python program exits when only daemon threads are left. The
217initial value is inherited from the creating thread. The flag can be set
Georg Brandlecd2afa2009-02-05 11:40:35 +0000218through the :attr:`daemon` property.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000219
220There is a "main thread" object; this corresponds to the initial thread of
221control in the Python program. It is not a daemon thread.
222
223There is the possibility that "dummy thread objects" are created. These are
224thread objects corresponding to "alien threads", which are threads of control
225started outside the threading module, such as directly from C code. Dummy
226thread objects have limited functionality; they are always considered alive and
227daemonic, and cannot be :meth:`join`\ ed. They are never deleted, since it is
228impossible to detect the termination of alien threads.
229
230
231.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={})
232
233 This constructor should always be called with keyword arguments. Arguments are:
234
235 *group* should be ``None``; reserved for future extension when a
236 :class:`ThreadGroup` class is implemented.
237
238 *target* is the callable object to be invoked by the :meth:`run` method.
239 Defaults to ``None``, meaning nothing is called.
240
241 *name* is the thread name. By default, a unique name is constructed of the form
242 "Thread-*N*" where *N* is a small decimal number.
243
244 *args* is the argument tuple for the target invocation. Defaults to ``()``.
245
246 *kwargs* is a dictionary of keyword arguments for the target invocation.
247 Defaults to ``{}``.
248
249 If the subclass overrides the constructor, it must make sure to invoke the base
250 class constructor (``Thread.__init__()``) before doing anything else to the
251 thread.
252
253
254.. method:: Thread.start()
255
256 Start the thread's activity.
257
258 It must be called at most once per thread object. It arranges for the object's
259 :meth:`run` method to be invoked in a separate thread of control.
260
261 This method will raise a :exc:`RuntimeException` if called more than once on the
262 same thread object.
263
264
265.. method:: Thread.run()
266
267 Method representing the thread's activity.
268
269 You may override this method in a subclass. The standard :meth:`run` method
270 invokes the callable object passed to the object's constructor as the *target*
271 argument, if any, with sequential and keyword arguments taken from the *args*
272 and *kwargs* arguments, respectively.
273
274
275.. method:: Thread.join([timeout])
276
277 Wait until the thread terminates. This blocks the calling thread until the
278 thread whose :meth:`join` method is called terminates -- either normally or
279 through an unhandled exception -- or until the optional timeout occurs.
280
281 When the *timeout* argument is present and not ``None``, it should be a floating
282 point number specifying a timeout for the operation in seconds (or fractions
283 thereof). As :meth:`join` always returns ``None``, you must call :meth:`isAlive`
284 after :meth:`join` to decide whether a timeout happened -- if the thread is
285 still alive, the :meth:`join` call timed out.
286
287 When the *timeout* argument is not present or ``None``, the operation will block
288 until the thread terminates.
289
290 A thread can be :meth:`join`\ ed many times.
291
292 :meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
293 the current thread as that would cause a deadlock. It is also an error to
294 :meth:`join` a thread before it has been started and attempts to do so
295 raises the same exception.
296
297
298.. method:: Thread.getName()
299 Thread.setName()
300
301 Old API for :attr:`~Thread.name`.
302
303
304.. attribute:: Thread.name
305
306 A string used for identification purposes only. It has no semantics.
307 Multiple threads may be given the same name. The initial name is set by the
308 constructor.
309
310
311.. attribute:: Thread.ident
312
313 The 'thread identifier' of this thread or ``None`` if the thread has not been
314 started. This is a nonzero integer. See the :func:`thread.get_ident()`
315 function. Thread identifiers may be recycled when a thread exits and another
316 thread is created. The identifier is available even after the thread has
317 exited.
318
319 .. versionadded:: 2.6
320
321
322.. method:: Thread.is_alive()
323 Thread.isAlive()
324
325 Return whether the thread is alive.
326
327 Roughly, a thread is alive from the moment the :meth:`start` method returns
328 until its :meth:`run` method terminates. The module function :func:`enumerate`
329 returns a list of all alive threads.
330
331
332.. method:: Thread.isDaemon()
333 Thread.setDaemon()
334
335 Old API for :attr:`~Thread.daemon`.
336
337
338.. attribute:: Thread.daemon
339
Georg Brandlecd2afa2009-02-05 11:40:35 +0000340 A boolean value indicating whether this thread is a daemon thread (True) or
341 not (False). This must be set before :meth:`start` is called, otherwise
342 :exc:`RuntimeError` is raised. Its initial value is inherited from the
343 creating thread; the main thread is not a daemon thread and therefore all
344 threads created in the main thread default to :attr:`daemon` = ``False``.
Georg Brandl01ba86a2008-11-06 10:20:49 +0000345
346 The entire Python program exits when no alive non-daemon threads are left.
347
348
Georg Brandl8ec7f652007-08-15 14:28:01 +0000349.. _lock-objects:
350
351Lock Objects
352------------
353
354A primitive lock is a synchronization primitive that is not owned by a
355particular thread when locked. In Python, it is currently the lowest level
356synchronization primitive available, implemented directly by the :mod:`thread`
357extension module.
358
359A primitive lock is in one of two states, "locked" or "unlocked". It is created
360in the unlocked state. It has two basic methods, :meth:`acquire` and
361:meth:`release`. When the state is unlocked, :meth:`acquire` changes the state
362to locked and returns immediately. When the state is locked, :meth:`acquire`
363blocks until a call to :meth:`release` in another thread changes it to unlocked,
364then the :meth:`acquire` call resets it to locked and returns. The
365:meth:`release` method should only be called in the locked state; it changes the
366state to unlocked and returns immediately. If an attempt is made to release an
367unlocked lock, a :exc:`RuntimeError` will be raised.
368
369When more than one thread is blocked in :meth:`acquire` waiting for the state to
370turn to unlocked, only one thread proceeds when a :meth:`release` call resets
371the state to unlocked; which one of the waiting threads proceeds is not defined,
372and may vary across implementations.
373
374All methods are executed atomically.
375
376
377.. method:: Lock.acquire([blocking=1])
378
379 Acquire a lock, blocking or non-blocking.
380
381 When invoked without arguments, block until the lock is unlocked, then set it to
382 locked, and return true.
383
384 When invoked with the *blocking* argument set to true, do the same thing as when
385 called without arguments, and return true.
386
387 When invoked with the *blocking* argument set to false, do not block. If a call
388 without an argument would block, return false immediately; otherwise, do the
389 same thing as when called without arguments, and return true.
390
391
392.. method:: Lock.release()
393
394 Release a lock.
395
396 When the lock is locked, reset it to unlocked, and return. If any other threads
397 are blocked waiting for the lock to become unlocked, allow exactly one of them
398 to proceed.
399
400 Do not call this method when the lock is unlocked.
401
402 There is no return value.
403
404
405.. _rlock-objects:
406
407RLock Objects
408-------------
409
410A reentrant lock is a synchronization primitive that may be acquired multiple
411times by the same thread. Internally, it uses the concepts of "owning thread"
412and "recursion level" in addition to the locked/unlocked state used by primitive
413locks. In the locked state, some thread owns the lock; in the unlocked state,
414no thread owns it.
415
416To lock the lock, a thread calls its :meth:`acquire` method; this returns once
417the thread owns the lock. To unlock the lock, a thread calls its
418:meth:`release` method. :meth:`acquire`/:meth:`release` call pairs may be
419nested; only the final :meth:`release` (the :meth:`release` of the outermost
420pair) resets the lock to unlocked and allows another thread blocked in
421:meth:`acquire` to proceed.
422
423
424.. method:: RLock.acquire([blocking=1])
425
426 Acquire a lock, blocking or non-blocking.
427
428 When invoked without arguments: if this thread already owns the lock, increment
429 the recursion level by one, and return immediately. Otherwise, if another
430 thread owns the lock, block until the lock is unlocked. Once the lock is
431 unlocked (not owned by any thread), then grab ownership, set the recursion level
432 to one, and return. If more than one thread is blocked waiting until the lock
433 is unlocked, only one at a time will be able to grab ownership of the lock.
434 There is no return value in this case.
435
436 When invoked with the *blocking* argument set to true, do the same thing as when
437 called without arguments, and return true.
438
439 When invoked with the *blocking* argument set to false, do not block. If a call
440 without an argument would block, return false immediately; otherwise, do the
441 same thing as when called without arguments, and return true.
442
443
444.. method:: RLock.release()
445
446 Release a lock, decrementing the recursion level. If after the decrement it is
447 zero, reset the lock to unlocked (not owned by any thread), and if any other
448 threads are blocked waiting for the lock to become unlocked, allow exactly one
449 of them to proceed. If after the decrement the recursion level is still
450 nonzero, the lock remains locked and owned by the calling thread.
451
452 Only call this method when the calling thread owns the lock. A
453 :exc:`RuntimeError` is raised if this method is called when the lock is
454 unlocked.
455
456 There is no return value.
457
458
459.. _condition-objects:
460
461Condition Objects
462-----------------
463
464A condition variable is always associated with some kind of lock; this can be
465passed in or one will be created by default. (Passing one in is useful when
466several condition variables must share the same lock.)
467
468A condition variable has :meth:`acquire` and :meth:`release` methods that call
469the corresponding methods of the associated lock. It also has a :meth:`wait`
470method, and :meth:`notify` and :meth:`notifyAll` methods. These three must only
471be called when the calling thread has acquired the lock, otherwise a
472:exc:`RuntimeError` is raised.
473
474The :meth:`wait` method releases the lock, and then blocks until it is awakened
475by a :meth:`notify` or :meth:`notifyAll` call for the same condition variable in
476another thread. Once awakened, it re-acquires the lock and returns. It is also
477possible to specify a timeout.
478
479The :meth:`notify` method wakes up one of the threads waiting for the condition
480variable, if any are waiting. The :meth:`notifyAll` method wakes up all threads
481waiting for the condition variable.
482
483Note: the :meth:`notify` and :meth:`notifyAll` methods don't release the lock;
484this means that the thread or threads awakened will not return from their
485:meth:`wait` call immediately, but only when the thread that called
486:meth:`notify` or :meth:`notifyAll` finally relinquishes ownership of the lock.
487
488Tip: the typical programming style using condition variables uses the lock to
489synchronize access to some shared state; threads that are interested in a
490particular change of state call :meth:`wait` repeatedly until they see the
491desired state, while threads that modify the state call :meth:`notify` or
492:meth:`notifyAll` when they change the state in such a way that it could
493possibly be a desired state for one of the waiters. For example, the following
494code is a generic producer-consumer situation with unlimited buffer capacity::
495
496 # Consume one item
497 cv.acquire()
498 while not an_item_is_available():
499 cv.wait()
500 get_an_available_item()
501 cv.release()
502
503 # Produce one item
504 cv.acquire()
505 make_an_item_available()
506 cv.notify()
507 cv.release()
508
509To choose between :meth:`notify` and :meth:`notifyAll`, consider whether one
510state change can be interesting for only one or several waiting threads. E.g.
511in a typical producer-consumer situation, adding one item to the buffer only
512needs to wake up one consumer thread.
513
514
515.. class:: Condition([lock])
516
517 If the *lock* argument is given and not ``None``, it must be a :class:`Lock` or
518 :class:`RLock` object, and it is used as the underlying lock. Otherwise, a new
519 :class:`RLock` object is created and used as the underlying lock.
520
521
522.. method:: Condition.acquire(*args)
523
524 Acquire the underlying lock. This method calls the corresponding method on the
525 underlying lock; the return value is whatever that method returns.
526
527
528.. method:: Condition.release()
529
530 Release the underlying lock. This method calls the corresponding method on the
531 underlying lock; there is no return value.
532
533
534.. method:: Condition.wait([timeout])
535
536 Wait until notified or until a timeout occurs. If the calling thread has not
537 acquired the lock when this method is called, a :exc:`RuntimeError` is raised.
538
539 This method releases the underlying lock, and then blocks until it is awakened
540 by a :meth:`notify` or :meth:`notifyAll` call for the same condition variable in
541 another thread, or until the optional timeout occurs. Once awakened or timed
542 out, it re-acquires the lock and returns.
543
544 When the *timeout* argument is present and not ``None``, it should be a floating
545 point number specifying a timeout for the operation in seconds (or fractions
546 thereof).
547
548 When the underlying lock is an :class:`RLock`, it is not released using its
549 :meth:`release` method, since this may not actually unlock the lock when it was
550 acquired multiple times recursively. Instead, an internal interface of the
551 :class:`RLock` class is used, which really unlocks it even when it has been
552 recursively acquired several times. Another internal interface is then used to
553 restore the recursion level when the lock is reacquired.
554
555
556.. method:: Condition.notify()
557
558 Wake up a thread waiting on this condition, if any. Wait until notified or until
559 a timeout occurs. If the calling thread has not acquired the lock when this
560 method is called, a :exc:`RuntimeError` is raised.
561
562 This method wakes up one of the threads waiting for the condition variable, if
563 any are waiting; it is a no-op if no threads are waiting.
564
565 The current implementation wakes up exactly one thread, if any are waiting.
566 However, it's not safe to rely on this behavior. A future, optimized
567 implementation may occasionally wake up more than one thread.
568
569 Note: the awakened thread does not actually return from its :meth:`wait` call
570 until it can reacquire the lock. Since :meth:`notify` does not release the
571 lock, its caller should.
572
573
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000574.. method:: Condition.notify_all()
Benjamin Petersonf4395602008-06-11 17:50:00 +0000575 Condition.notifyAll()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576
577 Wake up all threads waiting on this condition. This method acts like
578 :meth:`notify`, but wakes up all waiting threads instead of one. If the calling
579 thread has not acquired the lock when this method is called, a
580 :exc:`RuntimeError` is raised.
581
582
583.. _semaphore-objects:
584
585Semaphore Objects
586-----------------
587
588This is one of the oldest synchronization primitives in the history of computer
589science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he
590used :meth:`P` and :meth:`V` instead of :meth:`acquire` and :meth:`release`).
591
592A semaphore manages an internal counter which is decremented by each
593:meth:`acquire` call and incremented by each :meth:`release` call. The counter
594can never go below zero; when :meth:`acquire` finds that it is zero, it blocks,
595waiting until some other thread calls :meth:`release`.
596
597
598.. class:: Semaphore([value])
599
600 The optional argument gives the initial *value* for the internal counter; it
601 defaults to ``1``. If the *value* given is less than 0, :exc:`ValueError` is
602 raised.
603
604
605.. method:: Semaphore.acquire([blocking])
606
607 Acquire a semaphore.
608
609 When invoked without arguments: if the internal counter is larger than zero on
610 entry, decrement it by one and return immediately. If it is zero on entry,
611 block, waiting until some other thread has called :meth:`release` to make it
612 larger than zero. This is done with proper interlocking so that if multiple
613 :meth:`acquire` calls are blocked, :meth:`release` will wake exactly one of them
614 up. The implementation may pick one at random, so the order in which blocked
615 threads are awakened should not be relied on. There is no return value in this
616 case.
617
618 When invoked with *blocking* set to true, do the same thing as when called
619 without arguments, and return true.
620
621 When invoked with *blocking* set to false, do not block. If a call without an
622 argument would block, return false immediately; otherwise, do the same thing as
623 when called without arguments, and return true.
624
625
626.. method:: Semaphore.release()
627
628 Release a semaphore, incrementing the internal counter by one. When it was zero
629 on entry and another thread is waiting for it to become larger than zero again,
630 wake up that thread.
631
632
633.. _semaphore-examples:
634
635:class:`Semaphore` Example
636^^^^^^^^^^^^^^^^^^^^^^^^^^
637
638Semaphores are often used to guard resources with limited capacity, for example,
639a database server. In any situation where the size of the resource size is
640fixed, you should use a bounded semaphore. Before spawning any worker threads,
641your main thread would initialize the semaphore::
642
643 maxconnections = 5
644 ...
645 pool_sema = BoundedSemaphore(value=maxconnections)
646
647Once spawned, worker threads call the semaphore's acquire and release methods
648when they need to connect to the server::
649
650 pool_sema.acquire()
651 conn = connectdb()
652 ... use connection ...
653 conn.close()
654 pool_sema.release()
655
656The use of a bounded semaphore reduces the chance that a programming error which
657causes the semaphore to be released more than it's acquired will go undetected.
658
659
660.. _event-objects:
661
662Event Objects
663-------------
664
665This is one of the simplest mechanisms for communication between threads: one
666thread signals an event and other threads wait for it.
667
668An event object manages an internal flag that can be set to true with the
669:meth:`set` method and reset to false with the :meth:`clear` method. The
670:meth:`wait` method blocks until the flag is true.
671
672
673.. class:: Event()
674
675 The internal flag is initially false.
676
677
Benjamin Petersonf4395602008-06-11 17:50:00 +0000678.. method:: Event.is_set()
679 Event.isSet()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000680
681 Return true if and only if the internal flag is true.
682
683
684.. method:: Event.set()
685
686 Set the internal flag to true. All threads waiting for it to become true are
687 awakened. Threads that call :meth:`wait` once the flag is true will not block at
688 all.
689
690
691.. method:: Event.clear()
692
693 Reset the internal flag to false. Subsequently, threads calling :meth:`wait`
694 will block until :meth:`set` is called to set the internal flag to true again.
695
696
697.. method:: Event.wait([timeout])
698
Georg Brandlef660e82009-03-31 20:41:08 +0000699 Block until the internal flag is true. If the internal flag is true on entry,
700 return immediately. Otherwise, block until another thread calls :meth:`set`
701 to set the flag to true, or until the optional timeout occurs.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000702
703 When the timeout argument is present and not ``None``, it should be a floating
704 point number specifying a timeout for the operation in seconds (or fractions
705 thereof).
706
Georg Brandlef660e82009-03-31 20:41:08 +0000707 This method returns the internal flag on exit, so it will always return
708 ``True`` except if a timeout is given and the operation times out.
709
710 .. versionchanged:: 2.7
711 Previously, the method always returned ``None``.
712
Georg Brandl8ec7f652007-08-15 14:28:01 +0000713
Georg Brandl8ec7f652007-08-15 14:28:01 +0000714.. _timer-objects:
715
716Timer Objects
717-------------
718
719This class represents an action that should be run only after a certain amount
720of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
721and as such also functions as an example of creating custom threads.
722
723Timers are started, as with threads, by calling their :meth:`start` method. The
724timer can be stopped (before its action has begun) by calling the :meth:`cancel`
725method. The interval the timer will wait before executing its action may not be
726exactly the same as the interval specified by the user.
727
728For example::
729
730 def hello():
731 print "hello, world"
732
733 t = Timer(30.0, hello)
734 t.start() # after 30 seconds, "hello, world" will be printed
735
736
737.. class:: Timer(interval, function, args=[], kwargs={})
738
739 Create a timer that will run *function* with arguments *args* and keyword
740 arguments *kwargs*, after *interval* seconds have passed.
741
742
743.. method:: Timer.cancel()
744
745 Stop the timer, and cancel the execution of the timer's action. This will only
746 work if the timer is still in its waiting stage.
747
748
749.. _with-locks:
750
751Using locks, conditions, and semaphores in the :keyword:`with` statement
752------------------------------------------------------------------------
753
754All of the objects provided by this module that have :meth:`acquire` and
755:meth:`release` methods can be used as context managers for a :keyword:`with`
756statement. The :meth:`acquire` method will be called when the block is entered,
757and :meth:`release` will be called when the block is exited.
758
759Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
760:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
761:keyword:`with` statement context managers. For example::
762
Georg Brandl8ec7f652007-08-15 14:28:01 +0000763 import threading
764
765 some_rlock = threading.RLock()
766
767 with some_rlock:
768 print "some_rlock is locked while this executes"
769
Georg Brandl2e255512008-03-13 07:21:41 +0000770
771.. _threaded-imports:
772
773Importing in threaded code
774--------------------------
775
776While the import machinery is thread safe, there are two key
777restrictions on threaded imports due to inherent limitations in the way
778that thread safety is provided:
779
780* Firstly, other than in the main module, an import should not have the
781 side effect of spawning a new thread and then waiting for that thread in
782 any way. Failing to abide by this restriction can lead to a deadlock if
783 the spawned thread directly or indirectly attempts to import a module.
784* Secondly, all import attempts must be completed before the interpreter
785 starts shutting itself down. This can be most easily achieved by only
786 performing imports from non-daemon threads created through the threading
787 module. Daemon threads and threads created directly with the thread
788 module will require some other form of synchronization to ensure they do
789 not attempt imports after system shutdown has commenced. Failure to
790 abide by this restriction will lead to intermittent exceptions and
791 crashes during interpreter shutdown (as the late imports attempt to
792 access machinery which is no longer in a valid state).