2010-12-12 16:34:49 -04:00
|
|
|
:mod:`threading` --- Thread-based parallelism
|
|
|
|
=============================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. module:: threading
|
2010-12-12 16:34:49 -04:00
|
|
|
:synopsis: Thread-based parallelism.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-09 23:26:08 -04:00
|
|
|
**Source code:** :source:`Lib/threading.py`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-10 15:54:11 -04:00
|
|
|
--------------
|
|
|
|
|
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
|
|
|
|
2017-09-18 17:04:20 -03:00
|
|
|
.. versionchanged:: 3.7
|
|
|
|
This module used to be optional, it is now always available.
|
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
|
|
|
|
2011-01-06 12:31:28 -04:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
This module defines the following functions:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2019-05-27 19:39:52 -03:00
|
|
|
.. function:: excepthook(args, /)
|
|
|
|
|
|
|
|
Handle uncaught exception raised by :func:`Thread.run`.
|
|
|
|
|
|
|
|
The *args* argument has the following attributes:
|
|
|
|
|
|
|
|
* *exc_type*: Exception type.
|
|
|
|
* *exc_value*: Exception value, can be ``None``.
|
|
|
|
* *exc_traceback*: Exception traceback, can be ``None``.
|
|
|
|
* *thread*: Thread which raised the exception, can be ``None``.
|
|
|
|
|
|
|
|
If *exc_type* is :exc:`SystemExit`, the exception is silently ignored.
|
|
|
|
Otherwise, the exception is printed out on :data:`sys.stderr`.
|
|
|
|
|
|
|
|
If this function raises an exception, :func:`sys.excepthook` is called to
|
|
|
|
handle it.
|
|
|
|
|
|
|
|
:func:`threading.excepthook` can be overridden to control how uncaught
|
|
|
|
exceptions raised by :func:`Thread.run` are handled.
|
|
|
|
|
2019-06-14 13:03:22 -03:00
|
|
|
Storing *exc_value* using a custom hook can create a reference cycle. It
|
|
|
|
should be cleared explicitly to break the reference cycle when the
|
|
|
|
exception is no longer needed.
|
|
|
|
|
|
|
|
Storing *object* using a custom hook can resurrect it if it is set to an
|
|
|
|
object which is being finalized. Avoid storing *object* after the custom
|
|
|
|
hook completes to avoid resurrecting objects.
|
|
|
|
|
2019-05-27 19:39:52 -03:00
|
|
|
.. seealso::
|
|
|
|
:func:`sys.excepthook` handles uncaught exceptions.
|
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
|
|
|
|
2011-05-30 18:02:52 -03:00
|
|
|
.. function:: get_ident()
|
|
|
|
|
|
|
|
Return the 'thread identifier' of the current thread. This is a nonzero
|
|
|
|
integer. Its value has no direct meaning; it is intended as a magic cookie
|
|
|
|
to be used e.g. to index a dictionary of thread-specific data. Thread
|
|
|
|
identifiers may be recycled when a thread exits and another thread is
|
|
|
|
created.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
|
|
|
2019-05-22 12:43:17 -03:00
|
|
|
.. function:: get_native_id()
|
|
|
|
|
|
|
|
Return the native integral Thread ID of the current thread assigned by the kernel.
|
|
|
|
This is a non-negative integer.
|
|
|
|
Its value may be used to uniquely identify this particular thread system-wide
|
|
|
|
(until the thread terminates, after which the value may be recycled by the OS).
|
|
|
|
|
2019-06-13 19:34:46 -03:00
|
|
|
.. availability:: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX.
|
2019-05-22 12:43:17 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. 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
|
|
|
|
|
|
|
|
2013-09-04 01:01:07 -03:00
|
|
|
.. function:: main_thread()
|
|
|
|
|
|
|
|
Return the main :class:`Thread` object. In normal conditions, the
|
|
|
|
main thread is the thread from which the Python interpreter was
|
|
|
|
started.
|
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
|
|
|
|
|
|
|
|
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
|
2013-08-23 18:23:38 -03:00
|
|
|
:meth:`~Thread.run` method is called.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. 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
|
2013-08-23 18:23:38 -03:00
|
|
|
:meth:`~Thread.run` method is called.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. 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
|
2015-08-31 00:15:52 -03:00
|
|
|
integer value of at least 32,768 (32 KiB). If *size* is not specified,
|
|
|
|
0 is used. If changing the thread stack size is
|
2012-04-05 04:53:04 -03:00
|
|
|
unsupported, a :exc:`RuntimeError` is raised. If the specified stack size is
|
2013-02-16 11:29:56 -04:00
|
|
|
invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32 KiB
|
2007-08-15 11:28:22 -03:00
|
|
|
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
|
2013-02-16 11:29:56 -04:00
|
|
|
minimum stack size > 32 KiB or requiring allocation in multiples of the system
|
2007-08-15 11:28:22 -03:00
|
|
|
memory page size - platform documentation should be referred to for more
|
2013-02-16 11:29:56 -04:00
|
|
|
information (4 KiB pages are common; using multiples of 4096 for the stack size is
|
2007-08-15 11:28:22 -03:00
|
|
|
the suggested approach in the absence of more specific information).
|
2018-10-12 11:55:20 -03:00
|
|
|
|
|
|
|
.. availability:: Windows, systems with POSIX threads.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
This module defines a number of classes, which are detailed in the sections
|
|
|
|
below.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
Thread-Local Data
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Thread-local data is 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: local()
|
|
|
|
|
|
|
|
A class that represents thread-local data.
|
|
|
|
|
|
|
|
For more details and extensive examples, see the documentation string of the
|
|
|
|
:mod:`_threading_local` module.
|
|
|
|
|
|
|
|
|
2008-11-07 05:39:56 -04:00
|
|
|
.. _thread-objects:
|
|
|
|
|
|
|
|
Thread Objects
|
|
|
|
--------------
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
The :class:`Thread` 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:`~Thread.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:`~Thread.__init__` and :meth:`~Thread.run` methods of this class.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
Once a thread object is created, its activity must be started by calling the
|
2012-04-10 17:35:53 -03:00
|
|
|
thread's :meth:`~Thread.start` method. This invokes the :meth:`~Thread.run`
|
|
|
|
method in a separate thread of control.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
Once the thread's activity is started, the thread is considered 'alive'. It
|
2012-04-10 17:35:53 -03:00
|
|
|
stops being alive when its :meth:`~Thread.run` method terminates -- either
|
|
|
|
normally, or by raising an unhandled exception. The :meth:`~Thread.is_alive`
|
|
|
|
method tests whether the thread is alive.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
Other threads can call a thread's :meth:`~Thread.join` method. This blocks
|
|
|
|
the calling thread until the thread whose :meth:`~Thread.join` method is
|
|
|
|
called is terminated.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
A thread has a name. The name can be passed to the constructor, and read or
|
2012-04-10 17:35:53 -03:00
|
|
|
changed through the :attr:`~Thread.name` attribute.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2019-05-27 19:39:52 -03:00
|
|
|
If the :meth:`~Thread.run` method raises an exception,
|
|
|
|
:func:`threading.excepthook` is called to handle it. By default,
|
|
|
|
:func:`threading.excepthook` ignores silently :exc:`SystemExit`.
|
|
|
|
|
2008-11-07 05:39:56 -04:00
|
|
|
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
|
2012-04-10 17:51:26 -03:00
|
|
|
through the :attr:`~Thread.daemon` property or the *daemon* constructor
|
|
|
|
argument.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2013-02-15 16:27:18 -04:00
|
|
|
.. note::
|
|
|
|
Daemon threads are abruptly stopped at shutdown. Their resources (such
|
|
|
|
as open files, database transactions, etc.) may not be released properly.
|
|
|
|
If you want your threads to stop gracefully, make them non-daemonic and
|
|
|
|
use a suitable signalling mechanism such as an :class:`Event`.
|
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
daemonic, and cannot be :meth:`~Thread.join`\ ed. They are never deleted,
|
|
|
|
since it is impossible to detect the termination of alien threads.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
|
|
|
|
2012-09-08 14:49:18 -03:00
|
|
|
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
|
|
|
|
daemon=None)
|
2008-11-07 05:39:56 -04:00
|
|
|
|
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 ``{}``.
|
|
|
|
|
2011-02-25 18:07:43 -04:00
|
|
|
If not ``None``, *daemon* explicitly sets whether the thread is daemonic.
|
|
|
|
If ``None`` (the default), the daemonic property is inherited from the
|
|
|
|
current thread.
|
|
|
|
|
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
|
|
|
|
2019-06-14 13:55:22 -03:00
|
|
|
Daemon threads must not be used in subinterpreters.
|
|
|
|
|
2011-02-25 18:07:43 -04:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
Added the *daemon* argument.
|
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
object's :meth:`~Thread.run` method to be invoked in a separate thread
|
|
|
|
of control.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2011-01-31 15:35:02 -04:00
|
|
|
This method will raise a :exc:`RuntimeError` if called more than once
|
2009-07-26 11:48:09 -03:00
|
|
|
on the same thread object.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2019-06-14 13:55:22 -03:00
|
|
|
Raise a :exc:`RuntimeError` if the thread is a daemon thread and the
|
|
|
|
method is called from a subinterpreter.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.9
|
|
|
|
In a subinterpreter, spawning a daemon thread now raises an exception.
|
|
|
|
|
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
|
2019-04-23 10:01:09 -03:00
|
|
|
the *target* argument, if any, with positional and keyword arguments taken
|
2009-07-26 11:48:09 -03:00
|
|
|
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
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
Wait until the thread terminates. This blocks the calling thread until
|
|
|
|
the thread whose :meth:`~Thread.join` method is called terminates -- either
|
2016-12-24 03:28:26 -04:00
|
|
|
normally or through an unhandled exception -- or until the optional
|
2012-04-10 17:35:53 -03:00
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
(or fractions thereof). As :meth:`~Thread.join` always returns ``None``,
|
|
|
|
you must call :meth:`~Thread.is_alive` after :meth:`~Thread.join` to
|
|
|
|
decide whether a timeout happened -- if the thread is still alive, the
|
|
|
|
:meth:`~Thread.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
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
A thread can be :meth:`~Thread.join`\ ed many times.
|
2008-11-07 05:39:56 -04:00
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
:meth:`~Thread.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:`~Thread.join` a thread before it has been started
|
|
|
|
and attempts to do so raise 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
|
2017-09-27 03:13:15 -03:00
|
|
|
been started. This is a nonzero integer. See the :func:`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
|
|
|
|
2019-05-22 12:43:17 -03:00
|
|
|
.. attribute:: native_id
|
|
|
|
|
|
|
|
The native integral thread ID of this thread.
|
|
|
|
This is a non-negative integer, or ``None`` if the thread has not
|
|
|
|
been started. See the :func:`get_native_id` function.
|
|
|
|
This represents the Thread ID (``TID``) as assigned to the
|
|
|
|
thread by the OS (kernel). Its value may be used to uniquely identify
|
|
|
|
this particular thread system-wide (until the thread terminates,
|
|
|
|
after which the value may be recycled by the OS).
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Similar to Process IDs, Thread IDs are only valid (guaranteed unique
|
|
|
|
system-wide) from the time the thread is created until the thread
|
|
|
|
has been terminated.
|
|
|
|
|
2019-07-30 18:41:46 -03:00
|
|
|
.. availability:: Requires :func:`get_native_id` function.
|
2019-05-22 12:43:17 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
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
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
This method returns ``True`` just before the :meth:`~Thread.run` method
|
|
|
|
starts until just after the :meth:`~Thread.run` method terminates. The
|
|
|
|
module function :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)
|
2012-04-10 17:35:53 -03:00
|
|
|
or not (False). This must be set before :meth:`~Thread.start` is called,
|
2009-07-26 11:48:09 -03:00
|
|
|
otherwise :exc:`RuntimeError` is raised. Its initial value is inherited
|
|
|
|
from the creating thread; the main thread is not a daemon thread and
|
2012-04-10 17:35:53 -03:00
|
|
|
therefore all threads created in the main thread default to
|
|
|
|
:attr:`~Thread.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
|
|
|
|
|
|
|
|
2011-02-28 18:04:51 -04:00
|
|
|
.. impl-detail::
|
|
|
|
|
2013-01-18 13:58:47 -04:00
|
|
|
In CPython, due to the :term:`Global Interpreter Lock`, only one thread
|
2011-02-28 18:04:51 -04:00
|
|
|
can execute Python code at once (even though certain performance-oriented
|
|
|
|
libraries might overcome this limitation).
|
2013-01-18 13:58:47 -04:00
|
|
|
If you want your application to make better use of the computational
|
2011-02-28 18:04:51 -04:00
|
|
|
resources of multi-core machines, you are advised to use
|
|
|
|
:mod:`multiprocessing` or :class:`concurrent.futures.ProcessPoolExecutor`.
|
|
|
|
However, threading is still an appropriate model if you want to run
|
|
|
|
multiple I/O-bound tasks simultaneously.
|
|
|
|
|
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
in the unlocked state. It has two basic methods, :meth:`~Lock.acquire` and
|
|
|
|
:meth:`~Lock.release`. When the state is unlocked, :meth:`~Lock.acquire`
|
|
|
|
changes the state to locked and returns immediately. When the state is locked,
|
|
|
|
:meth:`~Lock.acquire` blocks until a call to :meth:`~Lock.release` in another
|
|
|
|
thread changes it to unlocked, then the :meth:`~Lock.acquire` call resets it
|
|
|
|
to locked and returns. The :meth:`~Lock.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.
|
|
|
|
|
2014-09-10 17:43:41 -03:00
|
|
|
Locks also support the :ref:`context management protocol <with-locks>`.
|
2012-04-10 17:47:55 -03:00
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the
|
|
|
|
state to turn to unlocked, only one thread proceeds when a :meth:`~Lock.release`
|
|
|
|
call resets the state to unlocked; which one of the waiting threads proceeds
|
|
|
|
is not defined, and may vary across implementations.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
All methods are executed atomically.
|
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. class:: Lock()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
The class implementing primitive lock objects. Once a thread has acquired a
|
|
|
|
lock, subsequent attempts to acquire it block, until it is released; any
|
|
|
|
thread may release it.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2017-05-31 21:14:19 -03:00
|
|
|
Note that ``Lock`` is actually a factory function which returns an instance
|
|
|
|
of the most efficient version of the concrete Lock class that is supported
|
|
|
|
by the platform.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. method:: acquire(blocking=True, timeout=-1)
|
2010-04-14 12:44:10 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
Acquire a lock, blocking or non-blocking.
|
2010-04-14 12:44:10 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
When invoked with the *blocking* argument set to ``True`` (the default),
|
|
|
|
block until the lock is unlocked, then set it to locked and return ``True``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
When invoked with the *blocking* argument set to ``False``, do not block.
|
|
|
|
If a call with *blocking* set to ``True`` would block, return ``False``
|
|
|
|
immediately; otherwise, set the lock to locked and return ``True``.
|
2010-12-15 18:59:16 -04:00
|
|
|
|
2012-10-06 15:35:35 -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*
|
2013-10-06 05:48:08 -03:00
|
|
|
and as long as the lock cannot be acquired. A *timeout* argument of ``-1``
|
2012-10-06 15:35:35 -03:00
|
|
|
specifies an unbounded wait. It is forbidden to specify a *timeout*
|
|
|
|
when *blocking* is false.
|
2010-08-17 12:07:14 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
The return value is ``True`` if the lock is acquired successfully,
|
|
|
|
``False`` if not (for example if the *timeout* expired).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The *timeout* parameter is new.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.2
|
2018-09-12 17:48:03 -03:00
|
|
|
Lock acquisition can now be interrupted by signals on POSIX if the
|
|
|
|
underlying threading implementation supports it.
|
2012-10-06 15:35:35 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: release()
|
|
|
|
|
|
|
|
Release a lock. This can be called from any thread, not only the thread
|
|
|
|
which has acquired the 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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
There is no return value.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _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.
|
|
|
|
|
2012-04-10 17:35:53 -03:00
|
|
|
To lock the lock, a thread calls its :meth:`~RLock.acquire` method; this
|
|
|
|
returns once the thread owns the lock. To unlock the lock, a thread calls
|
|
|
|
its :meth:`~Lock.release` method. :meth:`~Lock.acquire`/:meth:`~Lock.release`
|
|
|
|
call pairs may be nested; only the final :meth:`~Lock.release` (the
|
|
|
|
:meth:`~Lock.release` of the outermost pair) resets the lock to unlocked and
|
|
|
|
allows another thread blocked in :meth:`~Lock.acquire` to proceed.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-09-10 17:43:41 -03:00
|
|
|
Reentrant locks also support the :ref:`context management protocol <with-locks>`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. class:: RLock()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
This class implements reentrant lock objects. 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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
Note that ``RLock`` is actually a factory function which returns an instance
|
|
|
|
of the most efficient version of the concrete RLock class that is supported
|
|
|
|
by the platform.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. method:: acquire(blocking=True, timeout=-1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
Acquire a lock, blocking or non-blocking.
|
2010-04-14 12:44:10 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
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.
|
2010-04-19 11:05:51 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
When invoked with the *blocking* argument set to true, do the same thing as when
|
|
|
|
called without arguments, and return true.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The *timeout* parameter is new.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
|
|
|
|
.. method:: 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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _condition-objects:
|
|
|
|
|
|
|
|
Condition Objects
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
A condition variable is always associated with some kind of lock; this can be
|
2012-04-10 17:24:05 -03:00
|
|
|
passed in or one will be created by default. Passing one in is useful when
|
|
|
|
several condition variables must share the same lock. The lock is part of
|
|
|
|
the condition object: you don't have to track it separately.
|
|
|
|
|
2014-09-10 17:43:41 -03:00
|
|
|
A condition variable obeys the :ref:`context management protocol <with-locks>`:
|
2012-04-10 17:47:55 -03:00
|
|
|
using the ``with`` statement acquires the associated lock for the duration of
|
|
|
|
the enclosed block. The :meth:`~Condition.acquire` and
|
|
|
|
:meth:`~Condition.release` methods also call the corresponding methods of
|
|
|
|
the associated lock.
|
2012-04-10 17:24:05 -03:00
|
|
|
|
|
|
|
Other methods must be called with the associated lock held. The
|
|
|
|
:meth:`~Condition.wait` method releases the lock, and then blocks until
|
|
|
|
another thread awakens it by calling :meth:`~Condition.notify` or
|
|
|
|
:meth:`~Condition.notify_all`. Once awakened, :meth:`~Condition.wait`
|
|
|
|
re-acquires the lock and returns. It is also possible to specify a timeout.
|
|
|
|
|
|
|
|
The :meth:`~Condition.notify` method wakes up one of the threads waiting for
|
|
|
|
the condition variable, if any are waiting. The :meth:`~Condition.notify_all`
|
|
|
|
method wakes up all threads waiting for the condition variable.
|
|
|
|
|
|
|
|
Note: the :meth:`~Condition.notify` and :meth:`~Condition.notify_all` methods
|
|
|
|
don't release the lock; this means that the thread or threads awakened will
|
|
|
|
not return from their :meth:`~Condition.wait` call immediately, but only when
|
|
|
|
the thread that called :meth:`~Condition.notify` or :meth:`~Condition.notify_all`
|
|
|
|
finally relinquishes ownership of the lock.
|
|
|
|
|
|
|
|
The typical programming style using condition variables uses the lock to
|
2007-08-15 11:28:22 -03:00
|
|
|
synchronize access to some shared state; threads that are interested in a
|
2012-04-10 17:24:05 -03:00
|
|
|
particular change of state call :meth:`~Condition.wait` repeatedly until they
|
|
|
|
see the desired state, while threads that modify the state call
|
|
|
|
:meth:`~Condition.notify` or :meth:`~Condition.notify_all` when they change
|
|
|
|
the state in such a way that it could 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::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
# Consume one item
|
2012-04-10 17:24:05 -03:00
|
|
|
with cv:
|
|
|
|
while not an_item_is_available():
|
|
|
|
cv.wait()
|
|
|
|
get_an_available_item()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
# Produce one item
|
2012-04-10 17:24:05 -03:00
|
|
|
with cv:
|
|
|
|
make_an_item_available()
|
2012-04-11 14:37:56 -03:00
|
|
|
cv.notify()
|
2012-04-10 17:24:05 -03:00
|
|
|
|
|
|
|
The ``while`` loop checking for the application's condition is necessary
|
|
|
|
because :meth:`~Condition.wait` can return after an arbitrary long time,
|
2012-04-11 14:37:56 -03:00
|
|
|
and the condition which prompted the :meth:`~Condition.notify` call may
|
|
|
|
no longer hold true. This is inherent to multi-threaded programming. The
|
|
|
|
:meth:`~Condition.wait_for` method can be used to automate the condition
|
|
|
|
checking, and eases the computation of timeouts::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:24:05 -03:00
|
|
|
# Consume an item
|
|
|
|
with cv:
|
|
|
|
cv.wait_for(an_item_is_available)
|
|
|
|
get_an_available_item()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:24:05 -03:00
|
|
|
To choose between :meth:`~Condition.notify` and :meth:`~Condition.notify_all`,
|
|
|
|
consider whether one 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
|
|
|
|
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
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
This class implements condition variable objects. A condition variable
|
|
|
|
allows one or more threads to wait until they are notified by another thread.
|
|
|
|
|
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
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
changed from a factory function to a class.
|
|
|
|
|
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)
|
|
|
|
|
2016-10-19 12:30:05 -03:00
|
|
|
Wait until a condition evaluates to true. *predicate* should be a
|
2010-11-18 08:46:39 -04:00
|
|
|
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
|
2015-06-14 21:35:37 -03:00
|
|
|
held when called and is re-acquired on return. The predicate is evaluated
|
2010-11-18 08:46:39 -04:00
|
|
|
with the lock held.
|
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
2011-11-12 14:44:25 -04:00
|
|
|
.. method:: notify(n=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-12 14:44:25 -04:00
|
|
|
By default, wake up one thread waiting on this condition, if any. If the
|
|
|
|
calling thread has not acquired the lock when this method is called, a
|
2009-07-26 11:48:09 -03:00
|
|
|
:exc:`RuntimeError` is raised.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-12 14:44:25 -04:00
|
|
|
This method wakes up at most *n* of the threads waiting for the condition
|
|
|
|
variable; it is a no-op if no threads are waiting.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-12 14:44:25 -04:00
|
|
|
The current implementation wakes up exactly *n* threads, if at least *n*
|
|
|
|
threads are waiting. However, it's not safe to rely on this behavior.
|
|
|
|
A future, optimized implementation may occasionally wake up more than
|
|
|
|
*n* threads.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-12 14:44:25 -04:00
|
|
|
Note: an awakened thread does not actually return from its :meth:`wait`
|
2009-07-26 11:48:09 -03:00
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
used the names ``P()`` and ``V()`` instead of :meth:`~Semaphore.acquire` and
|
|
|
|
:meth:`~Semaphore.release`).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A semaphore manages an internal counter which is decremented by each
|
2012-04-10 17:35:53 -03:00
|
|
|
:meth:`~Semaphore.acquire` call and incremented by each :meth:`~Semaphore.release`
|
|
|
|
call. The counter can never go below zero; when :meth:`~Semaphore.acquire`
|
|
|
|
finds that it is zero, it blocks, waiting until some other thread calls
|
|
|
|
:meth:`~Semaphore.release`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-09-10 17:43:41 -03:00
|
|
|
Semaphores also support the :ref:`context management protocol <with-locks>`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2009-09-16 12:58:14 -03:00
|
|
|
.. class:: Semaphore(value=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2017-12-07 14:04:26 -04:00
|
|
|
This class implements semaphore objects. A semaphore manages an atomic
|
|
|
|
counter representing the number of :meth:`release` calls minus the number of
|
2012-10-06 15:35:35 -03:00
|
|
|
: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.
|
|
|
|
|
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.
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
changed from a factory function to a class.
|
|
|
|
|
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
|
|
|
|
2017-12-07 14:04:26 -04:00
|
|
|
When invoked without arguments:
|
|
|
|
|
|
|
|
* If the internal counter is larger than zero on entry, decrement it by
|
|
|
|
one and return true immediately.
|
|
|
|
* If the internal counter is zero on entry, block until awoken by a call to
|
|
|
|
:meth:`~Semaphore.release`. Once awoken (and the counter is greater
|
|
|
|
than 0), decrement the counter by 1 and return true. Exactly one
|
|
|
|
thread will be awoken by each call to :meth:`~Semaphore.release`. The
|
|
|
|
order in which threads are awoken should not be relied on.
|
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
|
2017-12-07 14:04:26 -04:00
|
|
|
without an argument would block, return false immediately; otherwise, do
|
|
|
|
the same thing as when called without arguments, and return true.
|
2010-04-17 20:51:58 -03:00
|
|
|
|
2016-10-19 10:29:26 -03:00
|
|
|
When invoked with a *timeout* other than ``None``, it will block for at
|
2010-04-17 20:51:58 -03:00
|
|
|
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
|
|
|
|
2019-08-29 05:45:19 -03:00
|
|
|
.. method:: release(n=1)
|
|
|
|
|
|
|
|
Release a semaphore, incrementing the internal counter by *n*. When it
|
|
|
|
was zero on entry and other threads are waiting for it to become larger
|
|
|
|
than zero again, wake up *n* of those threads.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-08-29 05:45:19 -03:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
Added the *n* parameter to release multiple waiting threads at once.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. class:: BoundedSemaphore(value=1)
|
|
|
|
|
|
|
|
Class implementing bounded semaphore objects. 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.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
changed from a factory function to a class.
|
|
|
|
|
|
|
|
|
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,
|
2011-01-06 15:28:18 -04:00
|
|
|
a database server. In any situation where the size of the resource is fixed,
|
|
|
|
you should use a bounded semaphore. Before spawning any worker threads, your
|
|
|
|
main thread would initialize the semaphore::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
maxconnections = 5
|
2012-10-06 15:35:35 -03:00
|
|
|
# ...
|
2007-08-15 11:28:22 -03:00
|
|
|
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::
|
|
|
|
|
2012-04-10 17:47:55 -03:00
|
|
|
with pool_sema:
|
|
|
|
conn = connectdb()
|
|
|
|
try:
|
2012-10-06 15:35:35 -03:00
|
|
|
# ... use connection ...
|
2012-04-10 17:47:55 -03:00
|
|
|
finally:
|
|
|
|
conn.close()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
:meth:`~Event.set` method and reset to false with the :meth:`~Event.clear`
|
|
|
|
method. The :meth:`~Event.wait` method blocks until the flag is true.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Event()
|
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
Class implementing event objects. An event manages a flag 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.
|
|
|
|
The flag is initially false.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
changed from a factory function to a class.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
: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
|
|
|
|
2012-01-07 13:24:56 -04:00
|
|
|
This method returns true if and only if the internal flag has been set to
|
|
|
|
true, either before the wait call or after the wait starts, 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.
|
|
|
|
|
2013-08-23 18:23:38 -03:00
|
|
|
Timers are started, as with threads, by calling their :meth:`~Timer.start`
|
|
|
|
method. The timer can be stopped (before its action has begun) by calling the
|
|
|
|
:meth:`~Timer.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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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)
|
2016-05-10 06:01:23 -03:00
|
|
|
t.start() # after 30 seconds, "hello, world" will be printed
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2013-03-30 18:19:38 -03:00
|
|
|
.. class:: Timer(interval, function, args=None, kwargs=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Create a timer that will run *function* with arguments *args* and keyword
|
|
|
|
arguments *kwargs*, after *interval* seconds have passed.
|
2016-10-19 10:29:26 -03:00
|
|
|
If *args* is ``None`` (the default) then an empty list will be used.
|
|
|
|
If *kwargs* is ``None`` (the default) then an empty dict will be used.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-06 15:35:35 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
changed from a factory function to a class.
|
|
|
|
|
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
|
2012-04-10 17:35:53 -03:00
|
|
|
the barrier by calling the :meth:`~Barrier.wait` method and will block until
|
2017-08-14 15:54:53 -03:00
|
|
|
all of the threads have made their :meth:`~Barrier.wait` calls. At this point,
|
|
|
|
the threads are released simultaneously.
|
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
|
2011-10-19 04:58:56 -03:00
|
|
|
provided, it is used in preference to any that was supplied to the class
|
2010-10-28 10:07:50 -03:00
|
|
|
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
|
2011-01-11 15:59:46 -04:00
|
|
|
for each thread. 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
|
|
|
|
2019-05-17 19:44:57 -03:00
|
|
|
Note that using this function may 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
|
2019-05-17 19:44:57 -03:00
|
|
|
this for example if one of the threads needs to abort, to avoid deadlocking the
|
2010-10-28 10:07:50 -03:00
|
|
|
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:
|
|
|
|
|
2018-12-19 02:09:46 -04:00
|
|
|
Using locks, conditions, and semaphores in the :keyword:`!with` statement
|
|
|
|
-------------------------------------------------------------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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`
|
2012-04-10 17:47:55 -03:00
|
|
|
statement. The :meth:`acquire` method will be called when the block is
|
|
|
|
entered, and :meth:`release` will be called when the block is exited. Hence,
|
|
|
|
the following snippet::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:47:55 -03:00
|
|
|
with some_lock:
|
|
|
|
# do something...
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:47:55 -03:00
|
|
|
is equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:47:55 -03:00
|
|
|
some_lock.acquire()
|
|
|
|
try:
|
|
|
|
# do something...
|
|
|
|
finally:
|
|
|
|
some_lock.release()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-04-10 17:47:55 -03:00
|
|
|
Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
|
|
|
|
:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
|
|
|
|
:keyword:`with` statement context managers.
|