2007-08-15 11:28:22 -03:00
|
|
|
:mod:`threading` --- Higher-level threading interface
|
|
|
|
=====================================================
|
|
|
|
|
|
|
|
.. module:: threading
|
|
|
|
:synopsis: Higher-level threading interface.
|
|
|
|
|
|
|
|
|
2008-05-25 10:05:15 -03:00
|
|
|
This module constructs higher-level threading interfaces on top of the lower
|
|
|
|
level :mod:`_thread` module. See also the :mod:`queue` module.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The :mod:`dummy_threading` module is provided for situations where
|
2008-05-25 10:05:15 -03:00
|
|
|
:mod:`threading` cannot be used because :mod:`_thread` is missing.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-08-18 19:38:41 -03:00
|
|
|
.. note::
|
|
|
|
|
2008-09-01 20:09:31 -03:00
|
|
|
While they are not listed below, the ``camelCase`` names used for some
|
|
|
|
methods and functions in this module in the Python 2.x series are still
|
|
|
|
supported by this module.
|
2008-08-18 19:38:41 -03:00
|
|
|
|
2010-11-16 15:13:50 -04:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Latest version of the :source:`threading module Python source code
|
|
|
|
<Lib/threading.py>`
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
This module defines the following functions and objects:
|
|
|
|
|
|
|
|
|
2008-06-11 16:14:14 -03:00
|
|
|
.. function:: active_count()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Return the number of :class:`Thread` objects currently alive. The returned
|
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
Typo fix.
........
r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
Remove an extraneous space in unittest documentation.
........
r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
Py_SetPythonHome uses static storage #6913
........
r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
Add Armin Ronacher.
........
r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
kill bare except
........
r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
pep 8 defaults
........
r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
rationalize a bit
........
r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
#6932: remove paragraph that advises relying on __del__ being called.
........
r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
use macros
........
r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6936: for interactive use, quit() is just fine.
........
r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
#6905: use better exception messages in inspect when the argument is of the wrong type.
........
r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
#6938: "ident" is always a string, so use a format code which works.
........
r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
Use str.format() to fix beginner's mistake with %-style string formatting.
........
r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
Optimize optimization and fix method name in docstring.
........
r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6925: rewrite docs for locals() and vars() a bit.
........
r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
#6930: clarify description about byteorder handling in UTF decoder routines.
........
r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
#6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
........
r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
Update bug tracker reference.
........
r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
#6946: fix duplicate index entries for datetime classes.
........
r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
Fix references to threading.enumerate().
........
r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
Add Doug.
........
r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
Add Mark Summerfield.
........
r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
Fix encoding name.
........
r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
fix typos/rephrase
........
r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
comment out ugly xxx
........
r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
Tidied up name of parameter in StreamHandler
........
r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
Closes issue 6606.
........
r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
test logging
........
2009-10-04 11:49:41 -03:00
|
|
|
count is equal to the length of the list returned by :func:`.enumerate`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: Condition()
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
A factory function that returns a new condition variable object. A condition
|
|
|
|
variable allows one or more threads to wait until they are notified by another
|
|
|
|
thread.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`condition-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-06-11 16:14:14 -03:00
|
|
|
.. function:: current_thread()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Return the current :class:`Thread` object, corresponding to the caller's thread
|
|
|
|
of control. If the caller's thread of control was not created through the
|
|
|
|
:mod:`threading` module, a dummy thread object with limited functionality is
|
|
|
|
returned.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: enumerate()
|
|
|
|
|
2008-06-11 16:14:14 -03:00
|
|
|
Return a list of all :class:`Thread` objects currently alive. The list
|
|
|
|
includes daemonic threads, dummy thread objects created by
|
|
|
|
:func:`current_thread`, and the main thread. It excludes terminated threads
|
|
|
|
and threads that have not yet been started.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: Event()
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
A factory function that returns a new event object. An event manages a flag
|
2009-07-26 12:02:41 -03:00
|
|
|
that can be set to true with the :meth:`~Event.set` method and reset to false
|
|
|
|
with the :meth:`clear` method. The :meth:`wait` method blocks until the flag
|
|
|
|
is true.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`event-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. class:: local
|
|
|
|
|
|
|
|
A class that represents thread-local data. Thread-local data are data whose
|
|
|
|
values are thread specific. To manage thread-local data, just create an
|
|
|
|
instance of :class:`local` (or a subclass) and store attributes on it::
|
|
|
|
|
|
|
|
mydata = threading.local()
|
|
|
|
mydata.x = 1
|
|
|
|
|
|
|
|
The instance's values will be different for separate threads.
|
|
|
|
|
|
|
|
For more details and extensive examples, see the documentation string of the
|
|
|
|
:mod:`_threading_local` module.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: Lock()
|
|
|
|
|
|
|
|
A factory function that returns a new primitive lock object. Once a thread has
|
|
|
|
acquired it, subsequent attempts to acquire it block, until it is released; any
|
|
|
|
thread may release it.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`lock-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. function:: RLock()
|
|
|
|
|
|
|
|
A factory function that returns a new reentrant lock object. A reentrant lock
|
|
|
|
must be released by the thread that acquired it. Once a thread has acquired a
|
|
|
|
reentrant lock, the same thread may acquire it again without blocking; the
|
|
|
|
thread must release it once for each time it has acquired it.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`rlock-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: Semaphore(value=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
:noindex:
|
|
|
|
|
|
|
|
A factory function that returns a new semaphore object. A semaphore manages a
|
|
|
|
counter representing the number of :meth:`release` calls minus the number of
|
|
|
|
:meth:`acquire` calls, plus an initial value. The :meth:`acquire` method blocks
|
|
|
|
if necessary until it can return without making the counter negative. If not
|
|
|
|
given, *value* defaults to 1.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`semaphore-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. function:: BoundedSemaphore(value=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A factory function that returns a new bounded semaphore object. A bounded
|
|
|
|
semaphore checks to make sure its current value doesn't exceed its initial
|
|
|
|
value. If it does, :exc:`ValueError` is raised. In most situations semaphores
|
|
|
|
are used to guard resources with limited capacity. If the semaphore is released
|
|
|
|
too many times it's a sign of a bug. If not given, *value* defaults to 1.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Thread
|
2010-08-26 11:30:15 -03:00
|
|
|
:noindex:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A class that represents a thread of control. This class can be safely
|
|
|
|
subclassed in a limited fashion.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`thread-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. class:: Timer
|
2010-08-26 11:30:15 -03:00
|
|
|
:noindex:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A thread that executes a function after a specified interval has passed.
|
|
|
|
|
2010-08-26 11:30:15 -03:00
|
|
|
See :ref:`timer-objects`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. function:: settrace(func)
|
|
|
|
|
|
|
|
.. index:: single: trace function
|
|
|
|
|
|
|
|
Set a trace function for all threads started from the :mod:`threading` module.
|
|
|
|
The *func* will be passed to :func:`sys.settrace` for each thread, before its
|
|
|
|
:meth:`run` method is called.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: setprofile(func)
|
|
|
|
|
|
|
|
.. index:: single: profile function
|
|
|
|
|
|
|
|
Set a profile function for all threads started from the :mod:`threading` module.
|
|
|
|
The *func* will be passed to :func:`sys.setprofile` for each thread, before its
|
|
|
|
:meth:`run` method is called.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: stack_size([size])
|
|
|
|
|
|
|
|
Return the thread stack size used when creating new threads. The optional
|
|
|
|
*size* argument specifies the stack size to be used for subsequently created
|
|
|
|
threads, and must be 0 (use platform or configured default) or a positive
|
|
|
|
integer value of at least 32,768 (32kB). If changing the thread stack size is
|
|
|
|
unsupported, a :exc:`ThreadError` is raised. If the specified stack size is
|
|
|
|
invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32kB
|
|
|
|
is currently the minimum supported stack size value to guarantee sufficient
|
|
|
|
stack space for the interpreter itself. Note that some platforms may have
|
|
|
|
particular restrictions on values for the stack size, such as requiring a
|
|
|
|
minimum stack size > 32kB or requiring allocation in multiples of the system
|
|
|
|
memory page size - platform documentation should be referred to for more
|
|
|
|
information (4kB pages are common; using multiples of 4096 for the stack size is
|
|
|
|
the suggested approach in the absence of more specific information).
|
|
|
|
Availability: Windows, systems with POSIX threads.
|
|
|
|
|
|
|
|
|
2010-04-14 12:44:10 -03:00
|
|
|
This module also defines the following constant:
|
|
|
|
|
|
|
|
.. data:: TIMEOUT_MAX
|
|
|
|
|
|
|
|
The maximum value allowed for the *timeout* parameter of blocking functions
|
|
|
|
(:meth:`Lock.acquire`, :meth:`RLock.acquire`, :meth:`Condition.wait`, etc.).
|
2010-09-21 11:48:28 -03:00
|
|
|
Specifying a timeout greater than this value will raise an
|
2010-04-14 12:44:10 -03:00
|
|
|
:exc:`OverflowError`.
|
|
|
|
|
2010-04-19 11:05:51 -03:00
|
|
|
.. versionadded:: 3.2
|
2010-04-14 12:44:10 -03:00
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
Detailed interfaces for the objects are documented below.
|
|
|
|
|
|
|
|
The design of this module is loosely based on Java's threading model. However,
|
|
|
|
where Java makes locks and condition variables basic behavior of every object,
|
|
|
|
they are separate objects in Python. Python's :class:`Thread` class supports a
|
|
|
|
subset of the behavior of Java's Thread class; currently, there are no
|
|
|
|
priorities, no thread groups, and threads cannot be destroyed, stopped,
|
|
|
|
suspended, resumed, or interrupted. The static methods of Java's Thread class,
|
|
|
|
when implemented, are mapped to module-level functions.
|
|
|
|
|
|
|
|
All of the methods described below are executed atomically.
|
|
|
|
|
|
|
|
|
2008-11-07 05:39:56 -04:00
|
|
|
.. _thread-objects:
|
|
|
|
|
|
|
|
Thread Objects
|
|
|
|
--------------
|
|
|
|
|
|
|
|
This class represents an activity that is run in a separate thread of control.
|
|
|
|
There are two ways to specify the activity: by passing a callable object to the
|
|
|
|
constructor, or by overriding the :meth:`run` method in a subclass. No other
|
|
|
|
methods (except for the constructor) should be overridden in a subclass. In
|
|
|
|
other words, *only* override the :meth:`__init__` and :meth:`run` methods of
|
|
|
|
this class.
|
|
|
|
|
|
|
|
Once a thread object is created, its activity must be started by calling the
|
|
|
|
thread's :meth:`start` method. This invokes the :meth:`run` method in a
|
|
|
|
separate thread of control.
|
|
|
|
|
|
|
|
Once the thread's activity is started, the thread is considered 'alive'. It
|
|
|
|
stops being alive when its :meth:`run` method terminates -- either normally, or
|
|
|
|
by raising an unhandled exception. The :meth:`is_alive` method tests whether the
|
|
|
|
thread is alive.
|
|
|
|
|
|
|
|
Other threads can call a thread's :meth:`join` method. This blocks the calling
|
|
|
|
thread until the thread whose :meth:`join` method is called is terminated.
|
|
|
|
|
|
|
|
A thread has a name. The name can be passed to the constructor, and read or
|
|
|
|
changed through the :attr:`name` attribute.
|
|
|
|
|
|
|
|
A thread can be flagged as a "daemon thread". The significance of this flag is
|
|
|
|
that the entire Python program exits when only daemon threads are left. The
|
|
|
|
initial value is inherited from the creating thread. The flag can be set
|
Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69129 | benjamin.peterson | 2009-01-30 19:42:55 -0600 (Fri, 30 Jan 2009) | 1 line
check the errno in bad fd cases
........
r69130 | andrew.kuchling | 2009-01-30 20:50:09 -0600 (Fri, 30 Jan 2009) | 1 line
Add a section
........
r69131 | andrew.kuchling | 2009-01-30 21:26:02 -0600 (Fri, 30 Jan 2009) | 1 line
Text edits and markup fixes
........
r69139 | mark.dickinson | 2009-01-31 10:44:04 -0600 (Sat, 31 Jan 2009) | 2 lines
Add an extra test for long <-> float hash equivalence.
........
r69140 | benjamin.peterson | 2009-01-31 10:52:03 -0600 (Sat, 31 Jan 2009) | 1 line
PyErr_BadInternalCall() raises a SystemError, not TypeError #5112
........
r69143 | benjamin.peterson | 2009-01-31 15:00:10 -0600 (Sat, 31 Jan 2009) | 1 line
I believe the intention here was to avoid a global lookup
........
r69154 | benjamin.peterson | 2009-01-31 16:33:02 -0600 (Sat, 31 Jan 2009) | 1 line
fix indentation in comment
........
r69155 | david.goodger | 2009-01-31 16:53:46 -0600 (Sat, 31 Jan 2009) | 1 line
markup fix
........
r69156 | gregory.p.smith | 2009-01-31 16:57:30 -0600 (Sat, 31 Jan 2009) | 4 lines
- Issue #5104: The socket module now raises OverflowError when 16-bit port and
protocol numbers are supplied outside the allowed 0-65536 range on bind()
and getservbyport().
........
r69157 | benjamin.peterson | 2009-01-31 17:43:25 -0600 (Sat, 31 Jan 2009) | 1 line
add explanatory comment
........
r69158 | benjamin.peterson | 2009-01-31 17:54:38 -0600 (Sat, 31 Jan 2009) | 1 line
more flags which only work for function blocks
........
r69159 | gregory.p.smith | 2009-01-31 18:16:01 -0600 (Sat, 31 Jan 2009) | 2 lines
Update doc wording as suggested in issue4903.
........
r69169 | guilherme.polo | 2009-01-31 20:56:16 -0600 (Sat, 31 Jan 2009) | 3 lines
Restore Tkinter.Tk._loadtk so this test doesn't fail for problems
related to ttk.
........
r69288 | georg.brandl | 2009-02-05 04:30:57 -0600 (Thu, 05 Feb 2009) | 1 line
#5153: fix typo in example.
........
r69289 | georg.brandl | 2009-02-05 04:37:07 -0600 (Thu, 05 Feb 2009) | 1 line
#5144: document that PySys_SetArgv prepends the script directory (or the empty string) to sys.path.
........
r69293 | georg.brandl | 2009-02-05 04:59:28 -0600 (Thu, 05 Feb 2009) | 1 line
#5059: fix example.
........
r69297 | georg.brandl | 2009-02-05 05:32:18 -0600 (Thu, 05 Feb 2009) | 1 line
#5015: document PythonHome API functions.
........
r69298 | georg.brandl | 2009-02-05 05:33:21 -0600 (Thu, 05 Feb 2009) | 1 line
#4827: fix callback example.
........
r69299 | georg.brandl | 2009-02-05 05:35:28 -0600 (Thu, 05 Feb 2009) | 1 line
#4820: use correct module for ctypes.util.
........
r69300 | georg.brandl | 2009-02-05 05:38:23 -0600 (Thu, 05 Feb 2009) | 1 line
#4563: disable alpha and roman lists, fixes wrong formatting of contributor list.
........
r69301 | georg.brandl | 2009-02-05 05:40:35 -0600 (Thu, 05 Feb 2009) | 1 line
#5031: fix Thread.daemon property docs.
........
r69348 | benjamin.peterson | 2009-02-05 19:47:31 -0600 (Thu, 05 Feb 2009) | 1 line
fix download link
........
2009-02-05 22:40:07 -04:00
|
|
|
through the :attr:`daemon` property.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
There is a "main thread" object; this corresponds to the initial thread of
|
|
|
|
control in the Python program. It is not a daemon thread.
|
|
|
|
|
|
|
|
There is the possibility that "dummy thread objects" are created. These are
|
|
|
|
thread objects corresponding to "alien threads", which are threads of control
|
|
|
|
started outside the threading module, such as directly from C code. Dummy
|
|
|
|
thread objects have limited functionality; they are always considered alive and
|
|
|
|
daemonic, and cannot be :meth:`join`\ ed. They are never deleted, since it is
|
|
|
|
impossible to detect the termination of alien threads.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={})
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
This constructor should always be called with keyword arguments. Arguments
|
|
|
|
are:
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
*group* should be ``None``; reserved for future extension when a
|
|
|
|
:class:`ThreadGroup` class is implemented.
|
|
|
|
|
|
|
|
*target* is the callable object to be invoked by the :meth:`run` method.
|
|
|
|
Defaults to ``None``, meaning nothing is called.
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
*name* is the thread name. By default, a unique name is constructed of the
|
|
|
|
form "Thread-*N*" where *N* is a small decimal number.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
*args* is the argument tuple for the target invocation. Defaults to ``()``.
|
|
|
|
|
|
|
|
*kwargs* is a dictionary of keyword arguments for the target invocation.
|
|
|
|
Defaults to ``{}``.
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
If the subclass overrides the constructor, it must make sure to invoke the
|
|
|
|
base class constructor (``Thread.__init__()``) before doing anything else to
|
|
|
|
the thread.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: start()
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Start the thread's activity.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
It must be called at most once per thread object. It arranges for the
|
|
|
|
object's :meth:`run` method to be invoked in a separate thread of control.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
This method will raise a :exc:`RuntimeException` if called more than once
|
|
|
|
on the same thread object.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: run()
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Method representing the thread's activity.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
You may override this method in a subclass. The standard :meth:`run`
|
|
|
|
method invokes the callable object passed to the object's constructor as
|
|
|
|
the *target* argument, if any, with sequential and keyword arguments taken
|
|
|
|
from the *args* and *kwargs* arguments, respectively.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. method:: join(timeout=None)
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Wait until the thread terminates. This blocks the calling thread until the
|
|
|
|
thread whose :meth:`join` method is called terminates -- either normally
|
|
|
|
or through an unhandled exception -- or until the optional timeout occurs.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When the *timeout* argument is present and not ``None``, it should be a
|
|
|
|
floating point number specifying a timeout for the operation in seconds
|
|
|
|
(or fractions thereof). As :meth:`join` always returns ``None``, you must
|
|
|
|
call :meth:`is_alive` after :meth:`join` to decide whether a timeout
|
|
|
|
happened -- if the thread is still alive, the :meth:`join` call timed out.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When the *timeout* argument is not present or ``None``, the operation will
|
|
|
|
block until the thread terminates.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
A thread can be :meth:`join`\ ed many times.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
:meth:`join` raises a :exc:`RuntimeError` if an attempt is made to join
|
|
|
|
the current thread as that would cause a deadlock. It is also an error to
|
|
|
|
:meth:`join` a thread before it has been started and attempts to do so
|
|
|
|
raises the same exception.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. attribute:: name
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
A string used for identification purposes only. It has no semantics.
|
|
|
|
Multiple threads may be given the same name. The initial name is set by
|
|
|
|
the constructor.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: getName()
|
|
|
|
setName()
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Old getter/setter API for :attr:`~Thread.name`; use it directly as a
|
|
|
|
property instead.
|
2009-01-02 16:10:05 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. attribute:: ident
|
2009-01-02 16:10:05 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
The 'thread identifier' of this thread or ``None`` if the thread has not
|
|
|
|
been started. This is a nonzero integer. See the
|
|
|
|
:func:`thread.get_ident()` function. Thread identifiers may be recycled
|
|
|
|
when a thread exits and another thread is created. The identifier is
|
|
|
|
available even after the thread has exited.
|
2009-01-02 16:10:05 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: is_alive()
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Return whether the thread is alive.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2010-07-23 09:26:35 -03:00
|
|
|
This method returns ``True`` just before the :meth:`run` method starts
|
|
|
|
until just after the :meth:`run` method terminates. The module function
|
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
Typo fix.
........
r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
Remove an extraneous space in unittest documentation.
........
r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
Py_SetPythonHome uses static storage #6913
........
r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
Add Armin Ronacher.
........
r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
kill bare except
........
r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
pep 8 defaults
........
r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
rationalize a bit
........
r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
#6932: remove paragraph that advises relying on __del__ being called.
........
r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
use macros
........
r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6936: for interactive use, quit() is just fine.
........
r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
#6905: use better exception messages in inspect when the argument is of the wrong type.
........
r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
#6938: "ident" is always a string, so use a format code which works.
........
r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
Use str.format() to fix beginner's mistake with %-style string formatting.
........
r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
Optimize optimization and fix method name in docstring.
........
r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6925: rewrite docs for locals() and vars() a bit.
........
r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
#6930: clarify description about byteorder handling in UTF decoder routines.
........
r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
#6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
........
r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
Update bug tracker reference.
........
r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
#6946: fix duplicate index entries for datetime classes.
........
r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
Fix references to threading.enumerate().
........
r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
Add Doug.
........
r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
Add Mark Summerfield.
........
r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
Fix encoding name.
........
r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
fix typos/rephrase
........
r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
comment out ugly xxx
........
r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
Tidied up name of parameter in StreamHandler
........
r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
Closes issue 6606.
........
r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
test logging
........
2009-10-04 11:49:41 -03:00
|
|
|
:func:`.enumerate` returns a list of all alive threads.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. attribute:: daemon
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
A boolean value indicating whether this thread is a daemon thread (True)
|
|
|
|
or not (False). This must be set before :meth:`start` is called,
|
|
|
|
otherwise :exc:`RuntimeError` is raised. Its initial value is inherited
|
|
|
|
from the creating thread; the main thread is not a daemon thread and
|
|
|
|
therefore all threads created in the main thread default to :attr:`daemon`
|
|
|
|
= ``False``.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
The entire Python program exits when no alive non-daemon threads are left.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: isDaemon()
|
|
|
|
setDaemon()
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Old getter/setter API for :attr:`~Thread.daemon`; use it directly as a
|
|
|
|
property instead.
|
2009-01-02 16:10:05 -04:00
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. _lock-objects:
|
|
|
|
|
|
|
|
Lock Objects
|
|
|
|
------------
|
|
|
|
|
|
|
|
A primitive lock is a synchronization primitive that is not owned by a
|
|
|
|
particular thread when locked. In Python, it is currently the lowest level
|
2008-05-25 10:05:15 -03:00
|
|
|
synchronization primitive available, implemented directly by the :mod:`_thread`
|
2007-08-15 11:28:22 -03:00
|
|
|
extension module.
|
|
|
|
|
|
|
|
A primitive lock is in one of two states, "locked" or "unlocked". It is created
|
|
|
|
in the unlocked state. It has two basic methods, :meth:`acquire` and
|
|
|
|
:meth:`release`. When the state is unlocked, :meth:`acquire` changes the state
|
|
|
|
to locked and returns immediately. When the state is locked, :meth:`acquire`
|
|
|
|
blocks until a call to :meth:`release` in another thread changes it to unlocked,
|
|
|
|
then the :meth:`acquire` call resets it to locked and returns. The
|
|
|
|
:meth:`release` method should only be called in the locked state; it changes the
|
|
|
|
state to unlocked and returns immediately. If an attempt is made to release an
|
|
|
|
unlocked lock, a :exc:`RuntimeError` will be raised.
|
|
|
|
|
|
|
|
When more than one thread is blocked in :meth:`acquire` waiting for the state to
|
|
|
|
turn to unlocked, only one thread proceeds when a :meth:`release` call resets
|
|
|
|
the state to unlocked; which one of the waiting threads proceeds is not defined,
|
|
|
|
and may vary across implementations.
|
|
|
|
|
|
|
|
All methods are executed atomically.
|
|
|
|
|
|
|
|
|
2010-04-14 12:44:10 -03:00
|
|
|
.. method:: Lock.acquire(blocking=True, timeout=-1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Acquire a lock, blocking or non-blocking.
|
|
|
|
|
|
|
|
When invoked without arguments, block until the lock is unlocked, then set it to
|
|
|
|
locked, and return true.
|
|
|
|
|
|
|
|
When invoked with the *blocking* argument set to true, do the same thing as when
|
|
|
|
called without arguments, and return true.
|
|
|
|
|
|
|
|
When invoked with the *blocking* argument set to false, do not block. If a call
|
|
|
|
without an argument would block, return false immediately; otherwise, do the
|
|
|
|
same thing as when called without arguments, and return true.
|
|
|
|
|
2010-04-14 12:44:10 -03:00
|
|
|
When invoked with the floating-point *timeout* argument set to a positive
|
|
|
|
value, block for at most the number of seconds specified by *timeout*
|
|
|
|
and as long as the lock cannot be acquired. A negative *timeout* argument
|
|
|
|
specifies an unbounded wait. It is forbidden to specify a *timeout*
|
|
|
|
when *blocking* is false.
|
|
|
|
|
|
|
|
The return value is ``True`` if the lock is acquired successfully,
|
|
|
|
``False`` if not (for example if the *timeout* expired).
|
|
|
|
|
2010-04-19 11:05:51 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The *timeout* parameter is new.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. method:: Lock.release()
|
|
|
|
|
|
|
|
Release a lock.
|
|
|
|
|
|
|
|
When the lock is locked, reset it to unlocked, and return. If any other threads
|
|
|
|
are blocked waiting for the lock to become unlocked, allow exactly one of them
|
|
|
|
to proceed.
|
|
|
|
|
|
|
|
Do not call this method when the lock is unlocked.
|
|
|
|
|
|
|
|
There is no return value.
|
|
|
|
|
|
|
|
|
|
|
|
.. _rlock-objects:
|
|
|
|
|
|
|
|
RLock Objects
|
|
|
|
-------------
|
|
|
|
|
|
|
|
A reentrant lock is a synchronization primitive that may be acquired multiple
|
|
|
|
times by the same thread. Internally, it uses the concepts of "owning thread"
|
|
|
|
and "recursion level" in addition to the locked/unlocked state used by primitive
|
|
|
|
locks. In the locked state, some thread owns the lock; in the unlocked state,
|
|
|
|
no thread owns it.
|
|
|
|
|
|
|
|
To lock the lock, a thread calls its :meth:`acquire` method; this returns once
|
|
|
|
the thread owns the lock. To unlock the lock, a thread calls its
|
|
|
|
:meth:`release` method. :meth:`acquire`/:meth:`release` call pairs may be
|
|
|
|
nested; only the final :meth:`release` (the :meth:`release` of the outermost
|
|
|
|
pair) resets the lock to unlocked and allows another thread blocked in
|
|
|
|
:meth:`acquire` to proceed.
|
|
|
|
|
|
|
|
|
2010-04-14 12:44:10 -03:00
|
|
|
.. method:: RLock.acquire(blocking=True, timeout=-1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Acquire a lock, blocking or non-blocking.
|
|
|
|
|
|
|
|
When invoked without arguments: if this thread already owns the lock, increment
|
|
|
|
the recursion level by one, and return immediately. Otherwise, if another
|
|
|
|
thread owns the lock, block until the lock is unlocked. Once the lock is
|
|
|
|
unlocked (not owned by any thread), then grab ownership, set the recursion level
|
|
|
|
to one, and return. If more than one thread is blocked waiting until the lock
|
|
|
|
is unlocked, only one at a time will be able to grab ownership of the lock.
|
|
|
|
There is no return value in this case.
|
|
|
|
|
|
|
|
When invoked with the *blocking* argument set to true, do the same thing as when
|
|
|
|
called without arguments, and return true.
|
|
|
|
|
|
|
|
When invoked with the *blocking* argument set to false, do not block. If a call
|
|
|
|
without an argument would block, return false immediately; otherwise, do the
|
|
|
|
same thing as when called without arguments, and return true.
|
|
|
|
|
2010-04-14 12:44:10 -03:00
|
|
|
When invoked with the floating-point *timeout* argument set to a positive
|
|
|
|
value, block for at most the number of seconds specified by *timeout*
|
|
|
|
and as long as the lock cannot be acquired. Return true if the lock has
|
|
|
|
been acquired, false if the timeout has elapsed.
|
|
|
|
|
2010-04-19 11:05:51 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The *timeout* parameter is new.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. method:: RLock.release()
|
|
|
|
|
|
|
|
Release a lock, decrementing the recursion level. If after the decrement it is
|
|
|
|
zero, reset the lock to unlocked (not owned by any thread), and if any other
|
|
|
|
threads are blocked waiting for the lock to become unlocked, allow exactly one
|
|
|
|
of them to proceed. If after the decrement the recursion level is still
|
|
|
|
nonzero, the lock remains locked and owned by the calling thread.
|
|
|
|
|
|
|
|
Only call this method when the calling thread owns the lock. A
|
|
|
|
:exc:`RuntimeError` is raised if this method is called when the lock is
|
|
|
|
unlocked.
|
|
|
|
|
|
|
|
There is no return value.
|
|
|
|
|
|
|
|
|
|
|
|
.. _condition-objects:
|
|
|
|
|
|
|
|
Condition Objects
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
A condition variable is always associated with some kind of lock; this can be
|
|
|
|
passed in or one will be created by default. (Passing one in is useful when
|
|
|
|
several condition variables must share the same lock.)
|
|
|
|
|
|
|
|
A condition variable has :meth:`acquire` and :meth:`release` methods that call
|
|
|
|
the corresponding methods of the associated lock. It also has a :meth:`wait`
|
2008-06-13 03:32:25 -03:00
|
|
|
method, and :meth:`notify` and :meth:`notify_all` methods. These three must only
|
2007-08-15 11:28:22 -03:00
|
|
|
be called when the calling thread has acquired the lock, otherwise a
|
|
|
|
:exc:`RuntimeError` is raised.
|
|
|
|
|
|
|
|
The :meth:`wait` method releases the lock, and then blocks until it is awakened
|
2008-06-13 03:32:25 -03:00
|
|
|
by a :meth:`notify` or :meth:`notify_all` call for the same condition variable in
|
2007-08-15 11:28:22 -03:00
|
|
|
another thread. Once awakened, it re-acquires the lock and returns. It is also
|
|
|
|
possible to specify a timeout.
|
|
|
|
|
|
|
|
The :meth:`notify` method wakes up one of the threads waiting for the condition
|
2008-06-13 03:32:25 -03:00
|
|
|
variable, if any are waiting. The :meth:`notify_all` method wakes up all threads
|
2007-08-15 11:28:22 -03:00
|
|
|
waiting for the condition variable.
|
|
|
|
|
2008-06-13 03:32:25 -03:00
|
|
|
Note: the :meth:`notify` and :meth:`notify_all` methods don't release the lock;
|
2007-08-15 11:28:22 -03:00
|
|
|
this means that the thread or threads awakened will not return from their
|
|
|
|
:meth:`wait` call immediately, but only when the thread that called
|
2008-06-13 03:32:25 -03:00
|
|
|
:meth:`notify` or :meth:`notify_all` finally relinquishes ownership of the lock.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Tip: the typical programming style using condition variables uses the lock to
|
|
|
|
synchronize access to some shared state; threads that are interested in a
|
|
|
|
particular change of state call :meth:`wait` repeatedly until they see the
|
|
|
|
desired state, while threads that modify the state call :meth:`notify` or
|
2008-06-13 03:32:25 -03:00
|
|
|
:meth:`notify_all` when they change the state in such a way that it could
|
2007-08-15 11:28:22 -03:00
|
|
|
possibly be a desired state for one of the waiters. For example, the following
|
|
|
|
code is a generic producer-consumer situation with unlimited buffer capacity::
|
|
|
|
|
|
|
|
# Consume one item
|
|
|
|
cv.acquire()
|
|
|
|
while not an_item_is_available():
|
|
|
|
cv.wait()
|
|
|
|
get_an_available_item()
|
|
|
|
cv.release()
|
|
|
|
|
|
|
|
# Produce one item
|
|
|
|
cv.acquire()
|
|
|
|
make_an_item_available()
|
|
|
|
cv.notify()
|
|
|
|
cv.release()
|
|
|
|
|
2008-06-13 03:32:25 -03:00
|
|
|
To choose between :meth:`notify` and :meth:`notify_all`, consider whether one
|
2007-08-15 11:28:22 -03:00
|
|
|
state change can be interesting for only one or several waiting threads. E.g.
|
|
|
|
in a typical producer-consumer situation, adding one item to the buffer only
|
|
|
|
needs to wake up one consumer thread.
|
|
|
|
|
2010-11-18 08:46:39 -04:00
|
|
|
Note: Condition variables can be, depending on the implementation, subject
|
|
|
|
to both spurious wakeups (when :meth:`wait` returns without a :meth:`notify`
|
|
|
|
call) and stolen wakeups (when another thread acquires the lock before the
|
|
|
|
awoken thread.) For this reason, it is always necessary to verify the state
|
|
|
|
the thread is waiting for when :meth:`wait` returns and optionally repeat
|
|
|
|
the call as often as necessary.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. class:: Condition(lock=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
|
|
|
|
or :class:`RLock` object, and it is used as the underlying lock. Otherwise,
|
|
|
|
a new :class:`RLock` object is created and used as the underlying lock.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: acquire(*args)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Acquire the underlying lock. This method calls the corresponding method on
|
|
|
|
the underlying lock; the return value is whatever that method returns.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: release()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Release the underlying lock. This method calls the corresponding method on
|
|
|
|
the underlying lock; there is no return value.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. method:: wait(timeout=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Wait until notified or until a timeout occurs. If the calling thread has
|
|
|
|
not acquired the lock when this method is called, a :exc:`RuntimeError` is
|
|
|
|
raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
This method releases the underlying lock, and then blocks until it is
|
|
|
|
awakened by a :meth:`notify` or :meth:`notify_all` call for the same
|
|
|
|
condition variable in another thread, or until the optional timeout
|
|
|
|
occurs. Once awakened or timed out, it re-acquires the lock and returns.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When the *timeout* argument is present and not ``None``, it should be a
|
|
|
|
floating point number specifying a timeout for the operation in seconds
|
|
|
|
(or fractions thereof).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When the underlying lock is an :class:`RLock`, it is not released using
|
|
|
|
its :meth:`release` method, since this may not actually unlock the lock
|
|
|
|
when it was acquired multiple times recursively. Instead, an internal
|
|
|
|
interface of the :class:`RLock` class is used, which really unlocks it
|
|
|
|
even when it has been recursively acquired several times. Another internal
|
|
|
|
interface is then used to restore the recursion level when the lock is
|
|
|
|
reacquired.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-10-28 06:03:20 -03:00
|
|
|
The return value is ``True`` unless a given *timeout* expired, in which
|
|
|
|
case it is ``False``.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Previously, the method always returned ``None``.
|
|
|
|
|
2010-11-18 08:46:39 -04:00
|
|
|
.. method:: wait_for(predicate, timeout=None)
|
|
|
|
|
|
|
|
Wait until a condition evaluates to True. *predicate* should be a
|
|
|
|
callable which result will be interpreted as a boolean value.
|
|
|
|
A *timeout* may be provided giving the maximum time to wait.
|
|
|
|
|
|
|
|
This utility method may call :meth:`wait` repeatedly until the predicate
|
|
|
|
is satisfied, or until a timeout occurs. The return value is
|
|
|
|
the last return value of the predicate and will evaluate to
|
|
|
|
``False`` if the method timed out.
|
|
|
|
|
|
|
|
Ignoring the timeout feature, calling this method is roughly equivalent to
|
|
|
|
writing::
|
|
|
|
|
|
|
|
while not predicate():
|
|
|
|
cv.wait()
|
|
|
|
|
|
|
|
Therefore, the same rules apply as with :meth:`wait`: The lock must be
|
|
|
|
held when called and is re-aquired on return. The predicate is evaluated
|
|
|
|
with the lock held.
|
|
|
|
|
|
|
|
Using this method, the consumer example above can be written thus::
|
|
|
|
|
|
|
|
with cv:
|
|
|
|
cv.wait_for(an_item_is_available)
|
|
|
|
get_an_available_item()
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: notify()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Wake up a thread waiting on this condition, if any. If the calling thread
|
|
|
|
has not acquired the lock when this method is called, a
|
|
|
|
:exc:`RuntimeError` is raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
This method wakes up one of the threads waiting for the condition
|
|
|
|
variable, if any are waiting; it is a no-op if no threads are waiting.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
The current implementation wakes up exactly one thread, if any are
|
|
|
|
waiting. However, it's not safe to rely on this behavior. A future,
|
|
|
|
optimized implementation may occasionally wake up more than one thread.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Note: the awakened thread does not actually return from its :meth:`wait`
|
|
|
|
call until it can reacquire the lock. Since :meth:`notify` does not
|
|
|
|
release the lock, its caller should.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: notify_all()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Wake up all threads waiting on this condition. This method acts like
|
|
|
|
:meth:`notify`, but wakes up all waiting threads instead of one. If the
|
|
|
|
calling thread has not acquired the lock when this method is called, a
|
|
|
|
:exc:`RuntimeError` is raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _semaphore-objects:
|
|
|
|
|
|
|
|
Semaphore Objects
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
This is one of the oldest synchronization primitives in the history of computer
|
|
|
|
science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he
|
|
|
|
used :meth:`P` and :meth:`V` instead of :meth:`acquire` and :meth:`release`).
|
|
|
|
|
|
|
|
A semaphore manages an internal counter which is decremented by each
|
|
|
|
:meth:`acquire` call and incremented by each :meth:`release` call. The counter
|
|
|
|
can never go below zero; when :meth:`acquire` finds that it is zero, it blocks,
|
|
|
|
waiting until some other thread calls :meth:`release`.
|
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. class:: Semaphore(value=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The optional argument gives the initial *value* for the internal counter; it
|
|
|
|
defaults to ``1``. If the *value* given is less than 0, :exc:`ValueError` is
|
|
|
|
raised.
|
|
|
|
|
2010-04-17 20:51:58 -03:00
|
|
|
.. method:: acquire(blocking=True, timeout=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Acquire a semaphore.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When invoked without arguments: if the internal counter is larger than
|
|
|
|
zero on entry, decrement it by one and return immediately. If it is zero
|
|
|
|
on entry, block, waiting until some other thread has called
|
|
|
|
:meth:`release` to make it larger than zero. This is done with proper
|
|
|
|
interlocking so that if multiple :meth:`acquire` calls are blocked,
|
|
|
|
:meth:`release` will wake exactly one of them up. The implementation may
|
|
|
|
pick one at random, so the order in which blocked threads are awakened
|
2010-04-17 20:51:58 -03:00
|
|
|
should not be relied on. Returns true (or blocks indefinitely).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When invoked with *blocking* set to false, do not block. If a call
|
2010-04-17 20:51:58 -03:00
|
|
|
without an argument would block, return false immediately; otherwise,
|
|
|
|
do the same thing as when called without arguments, and return true.
|
|
|
|
|
|
|
|
When invoked with a *timeout* other than None, it will block for at
|
|
|
|
most *timeout* seconds. If acquire does not complete successfully in
|
|
|
|
that interval, return false. Return true otherwise.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The *timeout* parameter is new.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: release()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Release a semaphore, incrementing the internal counter by one. When it
|
|
|
|
was zero on entry and another thread is waiting for it to become larger
|
|
|
|
than zero again, wake up that thread.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _semaphore-examples:
|
|
|
|
|
|
|
|
:class:`Semaphore` Example
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Semaphores are often used to guard resources with limited capacity, for example,
|
|
|
|
a database server. In any situation where the size of the resource size is
|
|
|
|
fixed, you should use a bounded semaphore. Before spawning any worker threads,
|
|
|
|
your main thread would initialize the semaphore::
|
|
|
|
|
|
|
|
maxconnections = 5
|
|
|
|
...
|
|
|
|
pool_sema = BoundedSemaphore(value=maxconnections)
|
|
|
|
|
|
|
|
Once spawned, worker threads call the semaphore's acquire and release methods
|
|
|
|
when they need to connect to the server::
|
|
|
|
|
|
|
|
pool_sema.acquire()
|
|
|
|
conn = connectdb()
|
|
|
|
... use connection ...
|
|
|
|
conn.close()
|
|
|
|
pool_sema.release()
|
|
|
|
|
|
|
|
The use of a bounded semaphore reduces the chance that a programming error which
|
|
|
|
causes the semaphore to be released more than it's acquired will go undetected.
|
|
|
|
|
|
|
|
|
|
|
|
.. _event-objects:
|
|
|
|
|
|
|
|
Event Objects
|
|
|
|
-------------
|
|
|
|
|
|
|
|
This is one of the simplest mechanisms for communication between threads: one
|
|
|
|
thread signals an event and other threads wait for it.
|
|
|
|
|
|
|
|
An event object manages an internal flag that can be set to true with the
|
2009-07-26 12:02:41 -03:00
|
|
|
:meth:`~Event.set` method and reset to false with the :meth:`clear` method. The
|
2007-08-15 11:28:22 -03:00
|
|
|
:meth:`wait` method blocks until the flag is true.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Event()
|
|
|
|
|
|
|
|
The internal flag is initially false.
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: is_set()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Return true if and only if the internal flag is true.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: set()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Set the internal flag to true. All threads waiting for it to become true
|
|
|
|
are awakened. Threads that call :meth:`wait` once the flag is true will
|
|
|
|
not block at all.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: clear()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Reset the internal flag to false. Subsequently, threads calling
|
2009-07-26 12:02:41 -03:00
|
|
|
:meth:`wait` will block until :meth:`.set` is called to set the internal
|
2009-07-26 11:48:09 -03:00
|
|
|
flag to true again.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. method:: wait(timeout=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Block until the internal flag is true. If the internal flag is true on
|
|
|
|
entry, return immediately. Otherwise, block until another thread calls
|
|
|
|
:meth:`set` to set the flag to true, or until the optional timeout occurs.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
When the timeout argument is present and not ``None``, it should be a
|
|
|
|
floating point number specifying a timeout for the operation in seconds
|
|
|
|
(or fractions thereof).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
This method returns the internal flag on exit, so it will always return
|
|
|
|
``True`` except if a timeout is given and the operation times out.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. versionchanged:: 3.1
|
|
|
|
Previously, the method always returned ``None``.
|
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70712 | benjamin.peterson | 2009-03-30 10:15:38 -0500 (Mon, 30 Mar 2009) | 1 line
don't rely on the order dict repr #5605
........
r70714 | brett.cannon | 2009-03-30 10:20:53 -0500 (Mon, 30 Mar 2009) | 1 line
Add an entry to developers.txt.
........
r70764 | martin.v.loewis | 2009-03-30 17:06:33 -0500 (Mon, 30 Mar 2009) | 2 lines
Add several VM developers.
........
r70765 | georg.brandl | 2009-03-30 17:09:34 -0500 (Mon, 30 Mar 2009) | 1 line
#5199: make warning about vars() assignment more visible.
........
r70769 | andrew.kuchling | 2009-03-30 17:29:53 -0500 (Mon, 30 Mar 2009) | 1 line
Remove comment
........
r70770 | andrew.kuchling | 2009-03-30 17:30:20 -0500 (Mon, 30 Mar 2009) | 1 line
Add several items and placeholders
........
r70771 | andrew.kuchling | 2009-03-30 17:31:11 -0500 (Mon, 30 Mar 2009) | 1 line
Many edits
........
r70773 | georg.brandl | 2009-03-30 17:43:00 -0500 (Mon, 30 Mar 2009) | 1 line
#5039: make it clear that the impl. note refers to CPython.
........
r70776 | andrew.kuchling | 2009-03-30 18:08:24 -0500 (Mon, 30 Mar 2009) | 1 line
typo fix
........
r70777 | andrew.kuchling | 2009-03-30 18:09:46 -0500 (Mon, 30 Mar 2009) | 1 line
Add more items
........
r70788 | andrew.kuchling | 2009-03-30 20:21:01 -0500 (Mon, 30 Mar 2009) | 1 line
Add various items
........
r70789 | georg.brandl | 2009-03-30 20:25:15 -0500 (Mon, 30 Mar 2009) | 1 line
Fix a wrong struct field assignment (docstring as closure).
........
r70824 | georg.brandl | 2009-03-31 10:43:20 -0500 (Tue, 31 Mar 2009) | 1 line
#5519: remove reference to Kodos, which seems dead.
........
r70828 | georg.brandl | 2009-03-31 10:50:16 -0500 (Tue, 31 Mar 2009) | 1 line
#5581: fget argument of abstractproperty is optional as well.
........
r70832 | georg.brandl | 2009-03-31 11:31:11 -0500 (Tue, 31 Mar 2009) | 1 line
#1386675: specify WindowsError as the exception, because it has a winerror attribute that EnvironmentError doesnt have.
........
r70836 | georg.brandl | 2009-03-31 11:50:25 -0500 (Tue, 31 Mar 2009) | 1 line
#5417: replace references to undocumented functions by ones to documented functions.
........
r70842 | georg.brandl | 2009-03-31 12:13:06 -0500 (Tue, 31 Mar 2009) | 1 line
#970783: document PyObject_Generic[GS]etAttr.
........
r70851 | georg.brandl | 2009-03-31 13:26:55 -0500 (Tue, 31 Mar 2009) | 1 line
#837577: note cryptic return value of spawn*e on invalid env dicts.
........
r70855 | georg.brandl | 2009-03-31 13:30:37 -0500 (Tue, 31 Mar 2009) | 1 line
#5245: note that PyRun_SimpleString doesnt return on SystemExit.
........
r70857 | georg.brandl | 2009-03-31 13:33:10 -0500 (Tue, 31 Mar 2009) | 1 line
#5227: note that Py_Main doesnt return on SystemExit.
........
r70866 | georg.brandl | 2009-03-31 14:06:57 -0500 (Tue, 31 Mar 2009) | 1 line
#4882: document named group behavior a bit better.
........
r70867 | georg.brandl | 2009-03-31 14:10:35 -0500 (Tue, 31 Mar 2009) | 1 line
#1096310: document usage of sys.__std*__ a bit better.
........
r70868 | georg.brandl | 2009-03-31 14:12:17 -0500 (Tue, 31 Mar 2009) | 1 line
#5190: export make_option in __all__.
........
r70869 | georg.brandl | 2009-03-31 14:14:42 -0500 (Tue, 31 Mar 2009) | 1 line
Fix-up unwanted change.
........
r70870 | georg.brandl | 2009-03-31 14:26:24 -0500 (Tue, 31 Mar 2009) | 1 line
#4411: document mro() and __mro__. (I hope I got it right.)
........
r70871 | georg.brandl | 2009-03-31 14:30:56 -0500 (Tue, 31 Mar 2009) | 1 line
#5618: fix typo.
........
r70872 | r.david.murray | 2009-03-31 14:31:17 -0500 (Tue, 31 Mar 2009) | 3 lines
Delete out-of-date and little-known README from the test
directory by consensus of devs at pycon sprint.
........
r70883 | georg.brandl | 2009-03-31 15:41:08 -0500 (Tue, 31 Mar 2009) | 1 line
#1674032: return value of flag from Event.wait(). OKed by Guido.
........
r70885 | tarek.ziade | 2009-03-31 15:48:31 -0500 (Tue, 31 Mar 2009) | 1 line
using log.warn for sys.stderr
........
r70893 | georg.brandl | 2009-03-31 15:56:32 -0500 (Tue, 31 Mar 2009) | 1 line
#1530012: move TQS section before raw strings.
........
r70894 | benjamin.peterson | 2009-03-31 16:06:30 -0500 (Tue, 31 Mar 2009) | 1 line
take the usual lock precautions around _active_limbo_lock
........
r70896 | georg.brandl | 2009-03-31 16:15:33 -0500 (Tue, 31 Mar 2009) | 1 line
#5598: document DocFileSuite *args argument.
........
r70897 | benjamin.peterson | 2009-03-31 16:34:42 -0500 (Tue, 31 Mar 2009) | 1 line
fix Thread.ident when it is the main thread or a dummy thread #5632
........
r70903 | georg.brandl | 2009-03-31 16:45:18 -0500 (Tue, 31 Mar 2009) | 1 line
#1676135: remove trailing slashes from --prefix argument.
........
r70905 | georg.brandl | 2009-03-31 17:03:40 -0500 (Tue, 31 Mar 2009) | 1 line
#5563: more documentation for bdist_msi.
........
r70906 | georg.brandl | 2009-03-31 17:11:53 -0500 (Tue, 31 Mar 2009) | 1 line
#1651995: fix _convert_ref for non-ASCII characters.
........
r70907 | georg.brandl | 2009-03-31 17:18:19 -0500 (Tue, 31 Mar 2009) | 1 line
#3427: document correct return type for urlopen().info().
........
r70915 | georg.brandl | 2009-03-31 17:40:16 -0500 (Tue, 31 Mar 2009) | 1 line
#5018: remove confusing paragraph.
........
r70927 | georg.brandl | 2009-03-31 18:01:27 -0500 (Tue, 31 Mar 2009) | 1 line
Dont shout to users.
........
r70933 | georg.brandl | 2009-03-31 19:04:33 -0500 (Tue, 31 Mar 2009) | 2 lines
Issue #5635: Fix running test_sys with tracing enabled.
........
r70951 | georg.brandl | 2009-04-01 09:02:27 -0500 (Wed, 01 Apr 2009) | 1 line
Add Maksim, who worked on several issues at the sprint.
........
r70960 | jesse.noller | 2009-04-01 11:42:19 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3270: document Listener address restrictions on windows
........
r70962 | brett.cannon | 2009-04-01 12:07:16 -0500 (Wed, 01 Apr 2009) | 2 lines
Ron DuPlain was given commit privileges at PyCon 2009 to work on 3to2.
........
r70963 | georg.brandl | 2009-04-01 12:46:01 -0500 (Wed, 01 Apr 2009) | 1 line
#5655: fix docstring oversight.
........
r70964 | brett.cannon | 2009-04-01 12:52:13 -0500 (Wed, 01 Apr 2009) | 2 lines
Paul Kippes was given commit privileges to work on 3to2.
........
r70998 | georg.brandl | 2009-04-01 16:54:21 -0500 (Wed, 01 Apr 2009) | 1 line
In Pdb, stop assigning values to __builtin__._ which interferes with the one commonly installed by gettext.
........
r71001 | brett.cannon | 2009-04-01 18:01:12 -0500 (Wed, 01 Apr 2009) | 3 lines
Add my initials to Misc/developers.txt. Names are now sorted by number of
characters in the person's name.
........
r71006 | georg.brandl | 2009-04-01 18:32:17 -0500 (Wed, 01 Apr 2009) | 1 line
Cache the f_locals dict of the current frame, since every access to frame.f_locals overrides its contents with the real locals which undoes modifications made by the debugging user.
........
r71008 | andrew.kuchling | 2009-04-01 19:02:14 -0500 (Wed, 01 Apr 2009) | 1 line
Typo fix
........
r71010 | benjamin.peterson | 2009-04-01 19:11:52 -0500 (Wed, 01 Apr 2009) | 1 line
fix markup
........
r71011 | benjamin.peterson | 2009-04-01 19:12:47 -0500 (Wed, 01 Apr 2009) | 1 line
this should be :noindex:
........
r71019 | georg.brandl | 2009-04-01 21:00:01 -0500 (Wed, 01 Apr 2009) | 1 line
Fix test_doctest, missed two assignments to curframe.
........
r71037 | r.david.murray | 2009-04-01 23:34:04 -0500 (Wed, 01 Apr 2009) | 6 lines
Clarify that datetime strftime does not produce leap seconds and datetime
strptime does not accept it in the strftime behavior section of the
datetime docs.
Closes issue 2568.
........
r71056 | georg.brandl | 2009-04-02 12:43:07 -0500 (Thu, 02 Apr 2009) | 2 lines
Actually the displayhook should print the repr.
........
r71094 | vinay.sajip | 2009-04-03 05:23:18 -0500 (Fri, 03 Apr 2009) | 1 line
Added warning about logging use from asynchronous signal handlers.
........
r71101 | andrew.kuchling | 2009-04-03 16:43:00 -0500 (Fri, 03 Apr 2009) | 1 line
Add some items
........
r71102 | andrew.kuchling | 2009-04-03 16:44:49 -0500 (Fri, 03 Apr 2009) | 1 line
Fix 'the the'; grammar fix
........
r71103 | andrew.kuchling | 2009-04-03 16:45:29 -0500 (Fri, 03 Apr 2009) | 1 line
Fix 'the the' duplication
........
r71106 | vinay.sajip | 2009-04-03 16:58:16 -0500 (Fri, 03 Apr 2009) | 1 line
Clarified warning about logging use from asynchronous signal handlers.
........
r71119 | raymond.hettinger | 2009-04-04 00:37:47 -0500 (Sat, 04 Apr 2009) | 1 line
Add helpful link.
........
r71123 | r.david.murray | 2009-04-04 01:39:56 -0500 (Sat, 04 Apr 2009) | 2 lines
Fix error in description of 'oct' (issue 5678).
........
r71149 | georg.brandl | 2009-04-04 08:42:39 -0500 (Sat, 04 Apr 2009) | 1 line
#5642: clarify map() compatibility to the builtin.
........
r71150 | georg.brandl | 2009-04-04 08:45:49 -0500 (Sat, 04 Apr 2009) | 1 line
#5601: clarify that webbrowser is not meant for file names.
........
r71203 | benjamin.peterson | 2009-04-04 18:46:34 -0500 (Sat, 04 Apr 2009) | 1 line
note how using iter* are unsafe while mutating and document iter(dict)
........
r71212 | georg.brandl | 2009-04-05 05:24:20 -0500 (Sun, 05 Apr 2009) | 1 line
#1742837: expand HTTP server docs, and fix SocketServer ones to document methods as methods, not functions.
........
r71214 | georg.brandl | 2009-04-05 05:29:57 -0500 (Sun, 05 Apr 2009) | 1 line
Normalize spelling of Mac OS X.
........
r71215 | georg.brandl | 2009-04-05 05:32:26 -0500 (Sun, 05 Apr 2009) | 1 line
Avoid sure signs of a diseased mind.
........
r71216 | georg.brandl | 2009-04-05 05:41:02 -0500 (Sun, 05 Apr 2009) | 1 line
#1718017: document the relation of os.path and the posixpath, ntpath etc. modules better.
........
r71217 | georg.brandl | 2009-04-05 05:48:47 -0500 (Sun, 05 Apr 2009) | 1 line
#1726172: dont raise an unexpected IndexError if a voidresp() call has an empty response.
........
r71221 | vinay.sajip | 2009-04-05 06:06:24 -0500 (Sun, 05 Apr 2009) | 1 line
Issue #5695: Moved logging.captureWarnings() call inside with statement in WarningsTest.test_warnings.
........
r71240 | georg.brandl | 2009-04-05 09:40:06 -0500 (Sun, 05 Apr 2009) | 1 line
#5370: doc update about unpickling objects with custom __getattr__ etc. methods.
........
2009-04-05 16:13:16 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. _timer-objects:
|
|
|
|
|
|
|
|
Timer Objects
|
|
|
|
-------------
|
|
|
|
|
|
|
|
This class represents an action that should be run only after a certain amount
|
|
|
|
of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
|
|
|
|
and as such also functions as an example of creating custom threads.
|
|
|
|
|
|
|
|
Timers are started, as with threads, by calling their :meth:`start` method. The
|
|
|
|
timer can be stopped (before its action has begun) by calling the :meth:`cancel`
|
|
|
|
method. The interval the timer will wait before executing its action may not be
|
|
|
|
exactly the same as the interval specified by the user.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
def hello():
|
2007-09-01 20:34:30 -03:00
|
|
|
print("hello, world")
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
t = Timer(30.0, hello)
|
|
|
|
t.start() # after 30 seconds, "hello, world" will be printed
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Timer(interval, function, args=[], kwargs={})
|
|
|
|
|
|
|
|
Create a timer that will run *function* with arguments *args* and keyword
|
|
|
|
arguments *kwargs*, after *interval* seconds have passed.
|
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
.. method:: cancel()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-07-26 11:48:09 -03:00
|
|
|
Stop the timer, and cancel the execution of the timer's action. This will
|
|
|
|
only work if the timer is still in its waiting stage.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2010-10-28 06:43:10 -03:00
|
|
|
Barrier Objects
|
|
|
|
---------------
|
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
|
|
|
This class provides a simple synchronization primitive for use by a fixed number
|
|
|
|
of threads that need to wait for each other. Each of the threads tries to pass
|
|
|
|
the barrier by calling the :meth:`wait` method and will block until all of the
|
|
|
|
threads have made the call. At this points, the threads are released
|
|
|
|
simultanously.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
The barrier can be reused any number of times for the same number of threads.
|
|
|
|
|
|
|
|
As an example, here is a simple way to synchronize a client and server thread::
|
|
|
|
|
|
|
|
b = Barrier(2, timeout=5)
|
2010-10-28 10:07:50 -03:00
|
|
|
|
|
|
|
def server():
|
2010-10-28 06:43:10 -03:00
|
|
|
start_server()
|
|
|
|
b.wait()
|
|
|
|
while True:
|
|
|
|
connection = accept_connection()
|
|
|
|
process_server_connection(connection)
|
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
def client():
|
2010-10-28 06:43:10 -03:00
|
|
|
b.wait()
|
|
|
|
while True:
|
2010-10-28 10:07:50 -03:00
|
|
|
connection = make_connection()
|
|
|
|
process_client_connection(connection)
|
|
|
|
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
.. class:: Barrier(parties, action=None, timeout=None)
|
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
Create a barrier object for *parties* number of threads. An *action*, when
|
|
|
|
provided, is a callable to be called by one of the threads when they are
|
|
|
|
released. *timeout* is the default timeout value if none is specified for
|
|
|
|
the :meth:`wait` method.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
.. method:: wait(timeout=None)
|
|
|
|
|
|
|
|
Pass the barrier. When all the threads party to the barrier have called
|
2010-10-28 10:07:50 -03:00
|
|
|
this function, they are all released simultaneously. If a *timeout* is
|
|
|
|
provided, is is used in preference to any that was supplied to the class
|
|
|
|
constructor.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
The return value is an integer in the range 0 to *parties* -- 1, different
|
2010-10-28 06:43:10 -03:00
|
|
|
for each thrad. This can be used to select a thread to do some special
|
2010-10-28 10:07:50 -03:00
|
|
|
housekeeping, e.g.::
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
i = barrier.wait()
|
|
|
|
if i == 0:
|
2010-10-28 10:07:50 -03:00
|
|
|
# Only one thread needs to print this
|
|
|
|
print("passed the barrier")
|
2010-10-28 06:43:10 -03:00
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
If an *action* was provided to the constructor, one of the threads will
|
|
|
|
have called it prior to being released. Should this call raise an error,
|
|
|
|
the barrier is put into the broken state.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
If the call times out, the barrier is put into the broken state.
|
|
|
|
|
|
|
|
This method may raise a :class:`BrokenBarrierError` exception if the
|
2010-10-28 10:07:50 -03:00
|
|
|
barrier is broken or reset while a thread is waiting.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
.. method:: reset()
|
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
Return the barrier to the default, empty state. Any threads waiting on it
|
|
|
|
will receive the :class:`BrokenBarrierError` exception.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
Note that using this function may can require some external
|
2010-10-28 10:07:50 -03:00
|
|
|
synchronization if there are other threads whose state is unknown. If a
|
|
|
|
barrier is broken it may be better to just leave it and create a new one.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
.. method:: abort()
|
|
|
|
|
|
|
|
Put the barrier into a broken state. This causes any active or future
|
2010-10-28 10:07:50 -03:00
|
|
|
calls to :meth:`wait` to fail with the :class:`BrokenBarrierError`. Use
|
|
|
|
this for example if one of the needs to abort, to avoid deadlocking the
|
|
|
|
application.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
It may be preferable to simply create the barrier with a sensible
|
2010-10-28 10:07:50 -03:00
|
|
|
*timeout* value to automatically guard against one of the threads going
|
|
|
|
awry.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
.. attribute:: parties
|
|
|
|
|
|
|
|
The number of threads required to pass the barrier.
|
|
|
|
|
|
|
|
.. attribute:: n_waiting
|
|
|
|
|
|
|
|
The number of threads currently waiting in the barrier.
|
|
|
|
|
|
|
|
.. attribute:: broken
|
|
|
|
|
|
|
|
A boolean that is ``True`` if the barrier is in the broken state.
|
|
|
|
|
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
.. exception:: BrokenBarrierError
|
2010-10-28 06:43:10 -03:00
|
|
|
|
2010-10-28 10:07:50 -03:00
|
|
|
This exception, a subclass of :exc:`RuntimeError`, is raised when the
|
|
|
|
:class:`Barrier` object is reset or broken.
|
2010-10-28 06:43:10 -03:00
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. _with-locks:
|
|
|
|
|
|
|
|
Using locks, conditions, and semaphores in the :keyword:`with` statement
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
|
|
|
|
All of the objects provided by this module that have :meth:`acquire` and
|
|
|
|
:meth:`release` methods can be used as context managers for a :keyword:`with`
|
|
|
|
statement. The :meth:`acquire` method will be called when the block is entered,
|
|
|
|
and :meth:`release` will be called when the block is exited.
|
|
|
|
|
|
|
|
Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
|
|
|
|
:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
|
|
|
|
:keyword:`with` statement context managers. For example::
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
some_rlock = threading.RLock()
|
|
|
|
|
|
|
|
with some_rlock:
|
2007-09-01 20:34:30 -03:00
|
|
|
print("some_rlock is locked while this executes")
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61239 | andrew.kuchling | 2008-03-05 01:44:41 +0100 (Wed, 05 Mar 2008) | 1 line
Add more items; add fragmentary notes
........
r61240 | amaury.forgeotdarc | 2008-03-05 02:50:33 +0100 (Wed, 05 Mar 2008) | 13 lines
Issue#2238: some syntax errors from *args or **kwargs expressions
would give bogus error messages, because of untested exceptions::
>>> f(**g(1=2))
XXX undetected error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
instead of the expected SyntaxError: keyword can't be an expression
Will backport.
........
r61241 | neal.norwitz | 2008-03-05 06:10:48 +0100 (Wed, 05 Mar 2008) | 3 lines
Remove the files/dirs after closing the DB so the tests work on Windows.
Patch from Trent Nelson. Also simplified removing a file by using test_support.
........
r61242 | neal.norwitz | 2008-03-05 06:14:18 +0100 (Wed, 05 Mar 2008) | 3 lines
Get this test to pass even when there is no sound card in the system.
Patch from Trent Nelson. (I can't test this.)
........
r61243 | neal.norwitz | 2008-03-05 06:20:44 +0100 (Wed, 05 Mar 2008) | 3 lines
Catch OSError when trying to remove a file in case removal fails. This
should prevent a failure in tearDown masking any real test failure.
........
r61244 | neal.norwitz | 2008-03-05 06:38:06 +0100 (Wed, 05 Mar 2008) | 5 lines
Make the timeout longer to give slow machines a chance to pass the test
before timing out. This doesn't change the duration of the test under
normal circumstances. This is targetted at fixing the spurious failures
on the FreeBSD buildbot primarily.
........
r61245 | neal.norwitz | 2008-03-05 06:49:03 +0100 (Wed, 05 Mar 2008) | 1 line
Tabs -> spaces
........
r61246 | neal.norwitz | 2008-03-05 06:50:20 +0100 (Wed, 05 Mar 2008) | 1 line
Use -u urlfetch to run more tests
........
r61247 | neal.norwitz | 2008-03-05 06:51:20 +0100 (Wed, 05 Mar 2008) | 1 line
test_smtplib sometimes reports leaks too, suppress it
........
r61248 | jeffrey.yasskin | 2008-03-05 07:19:56 +0100 (Wed, 05 Mar 2008) | 5 lines
Fix test_socketserver on Windows after r61099 added several signal.alarm()
calls (which don't exist on non-Unix platforms).
Thanks to Trent Nelson for the report and patch.
........
r61249 | georg.brandl | 2008-03-05 08:10:35 +0100 (Wed, 05 Mar 2008) | 2 lines
Fix some rst.
........
r61252 | thomas.heller | 2008-03-05 15:53:39 +0100 (Wed, 05 Mar 2008) | 2 lines
News entry for yesterdays commit.
........
r61253 | thomas.heller | 2008-03-05 16:34:29 +0100 (Wed, 05 Mar 2008) | 3 lines
Issue 1872: Changed the struct module typecode from 't' to '?', for
compatibility with PEP3118.
........
r61254 | skip.montanaro | 2008-03-05 17:41:09 +0100 (Wed, 05 Mar 2008) | 4 lines
Elaborate on the role of the altinstall target when installing multiple
versions.
........
r61255 | georg.brandl | 2008-03-05 20:31:44 +0100 (Wed, 05 Mar 2008) | 2 lines
#2239: PYTHONPATH delimiter is os.pathsep.
........
r61256 | raymond.hettinger | 2008-03-05 21:59:58 +0100 (Wed, 05 Mar 2008) | 1 line
C implementation of itertools.permutations().
........
r61257 | raymond.hettinger | 2008-03-05 22:04:32 +0100 (Wed, 05 Mar 2008) | 1 line
Small code cleanup.
........
r61260 | martin.v.loewis | 2008-03-05 23:24:31 +0100 (Wed, 05 Mar 2008) | 2 lines
cd PCbuild only after deleting all pyc files.
........
r61261 | raymond.hettinger | 2008-03-06 02:15:52 +0100 (Thu, 06 Mar 2008) | 1 line
Add examples.
........
r61262 | andrew.kuchling | 2008-03-06 02:36:27 +0100 (Thu, 06 Mar 2008) | 1 line
Add two items
........
r61263 | georg.brandl | 2008-03-06 07:47:18 +0100 (Thu, 06 Mar 2008) | 2 lines
#1725737: ignore other VC directories other than CVS and SVN's too.
........
r61264 | martin.v.loewis | 2008-03-06 07:55:22 +0100 (Thu, 06 Mar 2008) | 4 lines
Patch #2232: os.tmpfile might fail on Windows if the user has no
permission to create files in the root directory.
Will backport to 2.5.
........
r61269 | georg.brandl | 2008-03-06 08:19:15 +0100 (Thu, 06 Mar 2008) | 2 lines
Expand on re.split behavior with captured expressions.
........
r61270 | georg.brandl | 2008-03-06 08:22:09 +0100 (Thu, 06 Mar 2008) | 2 lines
Little clarification of assignments.
........
r61271 | georg.brandl | 2008-03-06 08:31:34 +0100 (Thu, 06 Mar 2008) | 2 lines
Add isinstance/issubclass to tutorial.
........
r61272 | georg.brandl | 2008-03-06 08:34:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Add missing NEWS entry for r61263.
........
r61273 | georg.brandl | 2008-03-06 08:41:16 +0100 (Thu, 06 Mar 2008) | 2 lines
#2225: return nonzero status code from py_compile if not all files could be compiled.
........
r61274 | georg.brandl | 2008-03-06 08:43:02 +0100 (Thu, 06 Mar 2008) | 2 lines
#2220: handle matching failure more gracefully.
........
r61275 | georg.brandl | 2008-03-06 08:45:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Bug #2220: handle rlcompleter attribute match failure more gracefully.
........
r61278 | martin.v.loewis | 2008-03-06 14:49:47 +0100 (Thu, 06 Mar 2008) | 1 line
Rely on x64 platform configuration when building _bsddb on AMD64.
........
r61279 | martin.v.loewis | 2008-03-06 14:50:28 +0100 (Thu, 06 Mar 2008) | 1 line
Update db-4.4.20 build procedure.
........
r61285 | raymond.hettinger | 2008-03-06 21:52:01 +0100 (Thu, 06 Mar 2008) | 1 line
More tests.
........
r61286 | raymond.hettinger | 2008-03-06 23:51:36 +0100 (Thu, 06 Mar 2008) | 1 line
Issue 2246: itertools grouper object did not participate in GC (should be backported).
........
r61288 | raymond.hettinger | 2008-03-07 02:33:20 +0100 (Fri, 07 Mar 2008) | 1 line
Tweak recipes and tests
........
r61289 | jeffrey.yasskin | 2008-03-07 07:22:15 +0100 (Fri, 07 Mar 2008) | 5 lines
Progress on issue #1193577 by adding a polling .shutdown() method to
SocketServers. The core of the patch was written by Pedro Werneck, but any bugs
are mine. I've also rearranged the code for timeouts in order to avoid
interfering with the shutdown poll.
........
r61290 | nick.coghlan | 2008-03-07 15:13:28 +0100 (Fri, 07 Mar 2008) | 1 line
Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files)
........
r61298 | andrew.kuchling | 2008-03-07 22:09:23 +0100 (Fri, 07 Mar 2008) | 1 line
Grammar fix
........
r61303 | georg.brandl | 2008-03-08 10:54:06 +0100 (Sat, 08 Mar 2008) | 2 lines
#2253: fix continue vs. finally docs.
........
r61304 | marc-andre.lemburg | 2008-03-08 11:01:43 +0100 (Sat, 08 Mar 2008) | 3 lines
Add new name for Mandrake: Mandriva.
........
r61305 | georg.brandl | 2008-03-08 11:05:24 +0100 (Sat, 08 Mar 2008) | 2 lines
#1533486: fix types in refcount intro.
........
r61312 | facundo.batista | 2008-03-08 17:50:27 +0100 (Sat, 08 Mar 2008) | 5 lines
Issue 1106316. post_mortem()'s parameter, traceback, is now
optional: it defaults to the traceback of the exception that is currently
being handled.
........
r61313 | jeffrey.yasskin | 2008-03-08 19:26:54 +0100 (Sat, 08 Mar 2008) | 2 lines
Add tests for with and finally performance to pybench.
........
r61314 | jeffrey.yasskin | 2008-03-08 21:08:21 +0100 (Sat, 08 Mar 2008) | 2 lines
Fix pybench for pythons < 2.6, tested back to 2.3.
........
r61317 | jeffrey.yasskin | 2008-03-08 22:35:15 +0100 (Sat, 08 Mar 2008) | 3 lines
Well that was dumb. platform.python_implementation returns a function, not a
string.
........
r61329 | georg.brandl | 2008-03-09 16:11:39 +0100 (Sun, 09 Mar 2008) | 2 lines
#2249: document assertTrue and assertFalse.
........
r61332 | neal.norwitz | 2008-03-09 20:03:42 +0100 (Sun, 09 Mar 2008) | 4 lines
Introduce a lock to fix a race condition which caused an exception in the test.
Some buildbots were consistently failing (e.g., amd64).
Also remove a couple of semi-colons.
........
r61344 | raymond.hettinger | 2008-03-11 01:19:07 +0100 (Tue, 11 Mar 2008) | 1 line
Add recipe to docs.
........
r61350 | guido.van.rossum | 2008-03-11 22:18:06 +0100 (Tue, 11 Mar 2008) | 3 lines
Fix the overflows in expandtabs(). "This time for sure!"
(Exploit at request.)
........
r61351 | raymond.hettinger | 2008-03-11 22:37:46 +0100 (Tue, 11 Mar 2008) | 1 line
Improve docs for itemgetter(). Show that it works with slices.
........
r61363 | georg.brandl | 2008-03-13 08:15:56 +0100 (Thu, 13 Mar 2008) | 2 lines
#2265: fix example.
........
r61364 | georg.brandl | 2008-03-13 08:17:14 +0100 (Thu, 13 Mar 2008) | 2 lines
#2270: fix typo.
........
r61365 | georg.brandl | 2008-03-13 08:21:41 +0100 (Thu, 13 Mar 2008) | 2 lines
#1720705: add docs about import/threading interaction, wording by Nick.
........
r61366 | andrew.kuchling | 2008-03-13 12:07:35 +0100 (Thu, 13 Mar 2008) | 1 line
Add class decorators
........
r61367 | raymond.hettinger | 2008-03-13 17:43:17 +0100 (Thu, 13 Mar 2008) | 1 line
Add 2-to-3 support for the itertools moved to builtins or renamed.
........
r61368 | raymond.hettinger | 2008-03-13 17:43:59 +0100 (Thu, 13 Mar 2008) | 1 line
Consistent tense.
........
r61369 | raymond.hettinger | 2008-03-13 20:03:51 +0100 (Thu, 13 Mar 2008) | 1 line
Issue 2274: Add heapq.heappushpop().
........
r61370 | raymond.hettinger | 2008-03-13 20:33:34 +0100 (Thu, 13 Mar 2008) | 1 line
Simplify the nlargest() code using heappushpop().
........
r61371 | brett.cannon | 2008-03-13 21:27:00 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_thread over to unittest. Commits GHOP 237.
Thanks Benjamin Peterson for the patch.
........
r61372 | brett.cannon | 2008-03-13 21:33:10 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_tokenize to doctest.
Done as GHOP 238 by Josip Dzolonga.
........
r61373 | brett.cannon | 2008-03-13 21:47:41 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_contains, test_crypt, and test_select to unittest.
Patch from GHOP 294 by David Marek.
........
r61374 | brett.cannon | 2008-03-13 22:02:16 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_gdbm to use unittest.
Closes issue #1960. Thanks Giampaolo Rodola.
........
r61375 | brett.cannon | 2008-03-13 22:09:28 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_fcntl to unittest.
Closes issue #2055. Thanks Giampaolo Rodola.
........
r61376 | raymond.hettinger | 2008-03-14 06:03:44 +0100 (Fri, 14 Mar 2008) | 1 line
Leave heapreplace() unchanged.
........
r61378 | martin.v.loewis | 2008-03-14 14:56:09 +0100 (Fri, 14 Mar 2008) | 2 lines
Patch #2284: add -x64 option to rt.bat.
........
r61379 | martin.v.loewis | 2008-03-14 14:57:59 +0100 (Fri, 14 Mar 2008) | 2 lines
Use -x64 flag.
........
r61382 | brett.cannon | 2008-03-14 15:03:10 +0100 (Fri, 14 Mar 2008) | 2 lines
Remove a bad test.
........
r61383 | mark.dickinson | 2008-03-14 15:23:37 +0100 (Fri, 14 Mar 2008) | 9 lines
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
across platforms: it should now raise OverflowError on all
platforms. (Previously it raised OverflowError only on
non IEEE 754 platforms.)
Also fix the (already existing) test for this behaviour
so that it actually raises TestFailed instead of just
referencing it.
........
r61387 | thomas.heller | 2008-03-14 22:06:21 +0100 (Fri, 14 Mar 2008) | 1 line
Remove unneeded initializer.
........
r61388 | martin.v.loewis | 2008-03-14 22:19:28 +0100 (Fri, 14 Mar 2008) | 2 lines
Run debug version, cd to PCbuild.
........
r61392 | georg.brandl | 2008-03-15 00:10:34 +0100 (Sat, 15 Mar 2008) | 2 lines
Remove obsolete paragraph. #2288.
........
r61395 | georg.brandl | 2008-03-15 01:20:19 +0100 (Sat, 15 Mar 2008) | 2 lines
Fix lots of broken links in the docs, found by Sphinx' external link checker.
........
r61396 | skip.montanaro | 2008-03-15 03:32:49 +0100 (Sat, 15 Mar 2008) | 1 line
note that fork and forkpty raise OSError on failure
........
r61402 | skip.montanaro | 2008-03-15 17:04:45 +0100 (Sat, 15 Mar 2008) | 1 line
add %f format to datetime - issue 1158
........
r61403 | skip.montanaro | 2008-03-15 17:07:11 +0100 (Sat, 15 Mar 2008) | 2 lines
.
........
2008-03-15 21:07:10 -03:00
|
|
|
|
|
|
|
.. _threaded-imports:
|
|
|
|
|
|
|
|
Importing in threaded code
|
|
|
|
--------------------------
|
|
|
|
|
2010-10-19 18:07:16 -03:00
|
|
|
While the import machinery is thread-safe, there are two key restrictions on
|
|
|
|
threaded imports due to inherent limitations in the way that thread-safety is
|
|
|
|
provided:
|
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61239 | andrew.kuchling | 2008-03-05 01:44:41 +0100 (Wed, 05 Mar 2008) | 1 line
Add more items; add fragmentary notes
........
r61240 | amaury.forgeotdarc | 2008-03-05 02:50:33 +0100 (Wed, 05 Mar 2008) | 13 lines
Issue#2238: some syntax errors from *args or **kwargs expressions
would give bogus error messages, because of untested exceptions::
>>> f(**g(1=2))
XXX undetected error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
instead of the expected SyntaxError: keyword can't be an expression
Will backport.
........
r61241 | neal.norwitz | 2008-03-05 06:10:48 +0100 (Wed, 05 Mar 2008) | 3 lines
Remove the files/dirs after closing the DB so the tests work on Windows.
Patch from Trent Nelson. Also simplified removing a file by using test_support.
........
r61242 | neal.norwitz | 2008-03-05 06:14:18 +0100 (Wed, 05 Mar 2008) | 3 lines
Get this test to pass even when there is no sound card in the system.
Patch from Trent Nelson. (I can't test this.)
........
r61243 | neal.norwitz | 2008-03-05 06:20:44 +0100 (Wed, 05 Mar 2008) | 3 lines
Catch OSError when trying to remove a file in case removal fails. This
should prevent a failure in tearDown masking any real test failure.
........
r61244 | neal.norwitz | 2008-03-05 06:38:06 +0100 (Wed, 05 Mar 2008) | 5 lines
Make the timeout longer to give slow machines a chance to pass the test
before timing out. This doesn't change the duration of the test under
normal circumstances. This is targetted at fixing the spurious failures
on the FreeBSD buildbot primarily.
........
r61245 | neal.norwitz | 2008-03-05 06:49:03 +0100 (Wed, 05 Mar 2008) | 1 line
Tabs -> spaces
........
r61246 | neal.norwitz | 2008-03-05 06:50:20 +0100 (Wed, 05 Mar 2008) | 1 line
Use -u urlfetch to run more tests
........
r61247 | neal.norwitz | 2008-03-05 06:51:20 +0100 (Wed, 05 Mar 2008) | 1 line
test_smtplib sometimes reports leaks too, suppress it
........
r61248 | jeffrey.yasskin | 2008-03-05 07:19:56 +0100 (Wed, 05 Mar 2008) | 5 lines
Fix test_socketserver on Windows after r61099 added several signal.alarm()
calls (which don't exist on non-Unix platforms).
Thanks to Trent Nelson for the report and patch.
........
r61249 | georg.brandl | 2008-03-05 08:10:35 +0100 (Wed, 05 Mar 2008) | 2 lines
Fix some rst.
........
r61252 | thomas.heller | 2008-03-05 15:53:39 +0100 (Wed, 05 Mar 2008) | 2 lines
News entry for yesterdays commit.
........
r61253 | thomas.heller | 2008-03-05 16:34:29 +0100 (Wed, 05 Mar 2008) | 3 lines
Issue 1872: Changed the struct module typecode from 't' to '?', for
compatibility with PEP3118.
........
r61254 | skip.montanaro | 2008-03-05 17:41:09 +0100 (Wed, 05 Mar 2008) | 4 lines
Elaborate on the role of the altinstall target when installing multiple
versions.
........
r61255 | georg.brandl | 2008-03-05 20:31:44 +0100 (Wed, 05 Mar 2008) | 2 lines
#2239: PYTHONPATH delimiter is os.pathsep.
........
r61256 | raymond.hettinger | 2008-03-05 21:59:58 +0100 (Wed, 05 Mar 2008) | 1 line
C implementation of itertools.permutations().
........
r61257 | raymond.hettinger | 2008-03-05 22:04:32 +0100 (Wed, 05 Mar 2008) | 1 line
Small code cleanup.
........
r61260 | martin.v.loewis | 2008-03-05 23:24:31 +0100 (Wed, 05 Mar 2008) | 2 lines
cd PCbuild only after deleting all pyc files.
........
r61261 | raymond.hettinger | 2008-03-06 02:15:52 +0100 (Thu, 06 Mar 2008) | 1 line
Add examples.
........
r61262 | andrew.kuchling | 2008-03-06 02:36:27 +0100 (Thu, 06 Mar 2008) | 1 line
Add two items
........
r61263 | georg.brandl | 2008-03-06 07:47:18 +0100 (Thu, 06 Mar 2008) | 2 lines
#1725737: ignore other VC directories other than CVS and SVN's too.
........
r61264 | martin.v.loewis | 2008-03-06 07:55:22 +0100 (Thu, 06 Mar 2008) | 4 lines
Patch #2232: os.tmpfile might fail on Windows if the user has no
permission to create files in the root directory.
Will backport to 2.5.
........
r61269 | georg.brandl | 2008-03-06 08:19:15 +0100 (Thu, 06 Mar 2008) | 2 lines
Expand on re.split behavior with captured expressions.
........
r61270 | georg.brandl | 2008-03-06 08:22:09 +0100 (Thu, 06 Mar 2008) | 2 lines
Little clarification of assignments.
........
r61271 | georg.brandl | 2008-03-06 08:31:34 +0100 (Thu, 06 Mar 2008) | 2 lines
Add isinstance/issubclass to tutorial.
........
r61272 | georg.brandl | 2008-03-06 08:34:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Add missing NEWS entry for r61263.
........
r61273 | georg.brandl | 2008-03-06 08:41:16 +0100 (Thu, 06 Mar 2008) | 2 lines
#2225: return nonzero status code from py_compile if not all files could be compiled.
........
r61274 | georg.brandl | 2008-03-06 08:43:02 +0100 (Thu, 06 Mar 2008) | 2 lines
#2220: handle matching failure more gracefully.
........
r61275 | georg.brandl | 2008-03-06 08:45:52 +0100 (Thu, 06 Mar 2008) | 2 lines
Bug #2220: handle rlcompleter attribute match failure more gracefully.
........
r61278 | martin.v.loewis | 2008-03-06 14:49:47 +0100 (Thu, 06 Mar 2008) | 1 line
Rely on x64 platform configuration when building _bsddb on AMD64.
........
r61279 | martin.v.loewis | 2008-03-06 14:50:28 +0100 (Thu, 06 Mar 2008) | 1 line
Update db-4.4.20 build procedure.
........
r61285 | raymond.hettinger | 2008-03-06 21:52:01 +0100 (Thu, 06 Mar 2008) | 1 line
More tests.
........
r61286 | raymond.hettinger | 2008-03-06 23:51:36 +0100 (Thu, 06 Mar 2008) | 1 line
Issue 2246: itertools grouper object did not participate in GC (should be backported).
........
r61288 | raymond.hettinger | 2008-03-07 02:33:20 +0100 (Fri, 07 Mar 2008) | 1 line
Tweak recipes and tests
........
r61289 | jeffrey.yasskin | 2008-03-07 07:22:15 +0100 (Fri, 07 Mar 2008) | 5 lines
Progress on issue #1193577 by adding a polling .shutdown() method to
SocketServers. The core of the patch was written by Pedro Werneck, but any bugs
are mine. I've also rearranged the code for timeouts in order to avoid
interfering with the shutdown poll.
........
r61290 | nick.coghlan | 2008-03-07 15:13:28 +0100 (Fri, 07 Mar 2008) | 1 line
Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files)
........
r61298 | andrew.kuchling | 2008-03-07 22:09:23 +0100 (Fri, 07 Mar 2008) | 1 line
Grammar fix
........
r61303 | georg.brandl | 2008-03-08 10:54:06 +0100 (Sat, 08 Mar 2008) | 2 lines
#2253: fix continue vs. finally docs.
........
r61304 | marc-andre.lemburg | 2008-03-08 11:01:43 +0100 (Sat, 08 Mar 2008) | 3 lines
Add new name for Mandrake: Mandriva.
........
r61305 | georg.brandl | 2008-03-08 11:05:24 +0100 (Sat, 08 Mar 2008) | 2 lines
#1533486: fix types in refcount intro.
........
r61312 | facundo.batista | 2008-03-08 17:50:27 +0100 (Sat, 08 Mar 2008) | 5 lines
Issue 1106316. post_mortem()'s parameter, traceback, is now
optional: it defaults to the traceback of the exception that is currently
being handled.
........
r61313 | jeffrey.yasskin | 2008-03-08 19:26:54 +0100 (Sat, 08 Mar 2008) | 2 lines
Add tests for with and finally performance to pybench.
........
r61314 | jeffrey.yasskin | 2008-03-08 21:08:21 +0100 (Sat, 08 Mar 2008) | 2 lines
Fix pybench for pythons < 2.6, tested back to 2.3.
........
r61317 | jeffrey.yasskin | 2008-03-08 22:35:15 +0100 (Sat, 08 Mar 2008) | 3 lines
Well that was dumb. platform.python_implementation returns a function, not a
string.
........
r61329 | georg.brandl | 2008-03-09 16:11:39 +0100 (Sun, 09 Mar 2008) | 2 lines
#2249: document assertTrue and assertFalse.
........
r61332 | neal.norwitz | 2008-03-09 20:03:42 +0100 (Sun, 09 Mar 2008) | 4 lines
Introduce a lock to fix a race condition which caused an exception in the test.
Some buildbots were consistently failing (e.g., amd64).
Also remove a couple of semi-colons.
........
r61344 | raymond.hettinger | 2008-03-11 01:19:07 +0100 (Tue, 11 Mar 2008) | 1 line
Add recipe to docs.
........
r61350 | guido.van.rossum | 2008-03-11 22:18:06 +0100 (Tue, 11 Mar 2008) | 3 lines
Fix the overflows in expandtabs(). "This time for sure!"
(Exploit at request.)
........
r61351 | raymond.hettinger | 2008-03-11 22:37:46 +0100 (Tue, 11 Mar 2008) | 1 line
Improve docs for itemgetter(). Show that it works with slices.
........
r61363 | georg.brandl | 2008-03-13 08:15:56 +0100 (Thu, 13 Mar 2008) | 2 lines
#2265: fix example.
........
r61364 | georg.brandl | 2008-03-13 08:17:14 +0100 (Thu, 13 Mar 2008) | 2 lines
#2270: fix typo.
........
r61365 | georg.brandl | 2008-03-13 08:21:41 +0100 (Thu, 13 Mar 2008) | 2 lines
#1720705: add docs about import/threading interaction, wording by Nick.
........
r61366 | andrew.kuchling | 2008-03-13 12:07:35 +0100 (Thu, 13 Mar 2008) | 1 line
Add class decorators
........
r61367 | raymond.hettinger | 2008-03-13 17:43:17 +0100 (Thu, 13 Mar 2008) | 1 line
Add 2-to-3 support for the itertools moved to builtins or renamed.
........
r61368 | raymond.hettinger | 2008-03-13 17:43:59 +0100 (Thu, 13 Mar 2008) | 1 line
Consistent tense.
........
r61369 | raymond.hettinger | 2008-03-13 20:03:51 +0100 (Thu, 13 Mar 2008) | 1 line
Issue 2274: Add heapq.heappushpop().
........
r61370 | raymond.hettinger | 2008-03-13 20:33:34 +0100 (Thu, 13 Mar 2008) | 1 line
Simplify the nlargest() code using heappushpop().
........
r61371 | brett.cannon | 2008-03-13 21:27:00 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_thread over to unittest. Commits GHOP 237.
Thanks Benjamin Peterson for the patch.
........
r61372 | brett.cannon | 2008-03-13 21:33:10 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_tokenize to doctest.
Done as GHOP 238 by Josip Dzolonga.
........
r61373 | brett.cannon | 2008-03-13 21:47:41 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_contains, test_crypt, and test_select to unittest.
Patch from GHOP 294 by David Marek.
........
r61374 | brett.cannon | 2008-03-13 22:02:16 +0100 (Thu, 13 Mar 2008) | 4 lines
Move test_gdbm to use unittest.
Closes issue #1960. Thanks Giampaolo Rodola.
........
r61375 | brett.cannon | 2008-03-13 22:09:28 +0100 (Thu, 13 Mar 2008) | 4 lines
Convert test_fcntl to unittest.
Closes issue #2055. Thanks Giampaolo Rodola.
........
r61376 | raymond.hettinger | 2008-03-14 06:03:44 +0100 (Fri, 14 Mar 2008) | 1 line
Leave heapreplace() unchanged.
........
r61378 | martin.v.loewis | 2008-03-14 14:56:09 +0100 (Fri, 14 Mar 2008) | 2 lines
Patch #2284: add -x64 option to rt.bat.
........
r61379 | martin.v.loewis | 2008-03-14 14:57:59 +0100 (Fri, 14 Mar 2008) | 2 lines
Use -x64 flag.
........
r61382 | brett.cannon | 2008-03-14 15:03:10 +0100 (Fri, 14 Mar 2008) | 2 lines
Remove a bad test.
........
r61383 | mark.dickinson | 2008-03-14 15:23:37 +0100 (Fri, 14 Mar 2008) | 9 lines
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
across platforms: it should now raise OverflowError on all
platforms. (Previously it raised OverflowError only on
non IEEE 754 platforms.)
Also fix the (already existing) test for this behaviour
so that it actually raises TestFailed instead of just
referencing it.
........
r61387 | thomas.heller | 2008-03-14 22:06:21 +0100 (Fri, 14 Mar 2008) | 1 line
Remove unneeded initializer.
........
r61388 | martin.v.loewis | 2008-03-14 22:19:28 +0100 (Fri, 14 Mar 2008) | 2 lines
Run debug version, cd to PCbuild.
........
r61392 | georg.brandl | 2008-03-15 00:10:34 +0100 (Sat, 15 Mar 2008) | 2 lines
Remove obsolete paragraph. #2288.
........
r61395 | georg.brandl | 2008-03-15 01:20:19 +0100 (Sat, 15 Mar 2008) | 2 lines
Fix lots of broken links in the docs, found by Sphinx' external link checker.
........
r61396 | skip.montanaro | 2008-03-15 03:32:49 +0100 (Sat, 15 Mar 2008) | 1 line
note that fork and forkpty raise OSError on failure
........
r61402 | skip.montanaro | 2008-03-15 17:04:45 +0100 (Sat, 15 Mar 2008) | 1 line
add %f format to datetime - issue 1158
........
r61403 | skip.montanaro | 2008-03-15 17:07:11 +0100 (Sat, 15 Mar 2008) | 2 lines
.
........
2008-03-15 21:07:10 -03:00
|
|
|
|
|
|
|
* Firstly, other than in the main module, an import should not have the
|
|
|
|
side effect of spawning a new thread and then waiting for that thread in
|
|
|
|
any way. Failing to abide by this restriction can lead to a deadlock if
|
|
|
|
the spawned thread directly or indirectly attempts to import a module.
|
|
|
|
* Secondly, all import attempts must be completed before the interpreter
|
|
|
|
starts shutting itself down. This can be most easily achieved by only
|
|
|
|
performing imports from non-daemon threads created through the threading
|
|
|
|
module. Daemon threads and threads created directly with the thread
|
|
|
|
module will require some other form of synchronization to ensure they do
|
|
|
|
not attempt imports after system shutdown has commenced. Failure to
|
|
|
|
abide by this restriction will lead to intermittent exceptions and
|
|
|
|
crashes during interpreter shutdown (as the late imports attempt to
|
|
|
|
access machinery which is no longer in a valid state).
|