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
|
|
|
|
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
|
|
|
|
count is equal to the length of the list returned by :func:`enumerate`.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
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
|
|
|
|
that can be set to true with the :meth:`set` method and reset to false with the
|
|
|
|
:meth:`clear` method. The :meth:`wait` method blocks until the flag is true.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: Semaphore([value])
|
|
|
|
: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.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: BoundedSemaphore([value])
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
A class that represents a thread of control. This class can be safely
|
|
|
|
subclassed in a limited fashion.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Timer
|
|
|
|
|
|
|
|
A thread that executes a function after a specified interval has passed.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
|
|
|
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={})
|
|
|
|
|
|
|
|
This constructor should always be called with keyword arguments. Arguments are:
|
|
|
|
|
|
|
|
*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.
|
|
|
|
|
|
|
|
*name* is the thread name. By default, a unique name is constructed of the form
|
|
|
|
"Thread-*N*" where *N* is a small decimal number.
|
|
|
|
|
|
|
|
*args* is the argument tuple for the target invocation. Defaults to ``()``.
|
|
|
|
|
|
|
|
*kwargs* is a dictionary of keyword arguments for the target invocation.
|
|
|
|
Defaults to ``{}``.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Thread.start()
|
|
|
|
|
|
|
|
Start the thread's activity.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
This method will raise a :exc:`RuntimeException` if called more than once on the
|
|
|
|
same thread object.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Thread.run()
|
|
|
|
|
|
|
|
Method representing the thread's activity.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Thread.join([timeout])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
When the *timeout* argument is not present or ``None``, the operation will block
|
|
|
|
until the thread terminates.
|
|
|
|
|
|
|
|
A thread can be :meth:`join`\ ed many times.
|
|
|
|
|
|
|
|
: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.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: Thread.name
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2009-01-02 16:10:05 -04:00
|
|
|
.. method:: Thread.getName()
|
|
|
|
Thread.setName()
|
|
|
|
|
|
|
|
Old getter/setter API for :attr:`~Thread.name`; use it directly as a property
|
|
|
|
instead.
|
|
|
|
|
|
|
|
|
2008-11-07 05:39:56 -04:00
|
|
|
.. attribute:: Thread.ident
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Thread.is_alive()
|
|
|
|
|
|
|
|
Return whether the thread is alive.
|
|
|
|
|
|
|
|
Roughly, a thread is alive from the moment the :meth:`start` method returns
|
|
|
|
until its :meth:`run` method terminates. The module function :func:`enumerate`
|
|
|
|
returns a list of all alive threads.
|
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: Thread.daemon
|
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
The entire Python program exits when no alive non-daemon threads are left.
|
|
|
|
|
|
|
|
|
2009-01-02 16:10:05 -04:00
|
|
|
.. method:: Thread.isDaemon()
|
|
|
|
Thread.setDaemon()
|
|
|
|
|
|
|
|
Old getter/setter API for :attr:`~Thread.daemon`; use it directly as a
|
|
|
|
property instead.
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Lock.acquire([blocking=1])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: RLock.acquire([blocking=1])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Condition([lock])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Condition.acquire(*args)
|
|
|
|
|
|
|
|
Acquire the underlying lock. This method calls the corresponding method on the
|
|
|
|
underlying lock; the return value is whatever that method returns.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Condition.release()
|
|
|
|
|
|
|
|
Release the underlying lock. This method calls the corresponding method on the
|
|
|
|
underlying lock; there is no return value.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Condition.wait([timeout])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
This method releases the underlying 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, or until the optional timeout occurs. Once awakened or timed
|
|
|
|
out, it re-acquires the lock and returns.
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Condition.notify()
|
|
|
|
|
|
|
|
Wake up a thread waiting on this condition, if any. 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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2008-06-11 16:14:14 -03:00
|
|
|
.. method:: Condition.notify_all()
|
2007-08-15 11:28:22 -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.
|
|
|
|
|
|
|
|
|
|
|
|
.. _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`.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Semaphore([value])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Semaphore.acquire([blocking])
|
|
|
|
|
|
|
|
Acquire a semaphore.
|
|
|
|
|
|
|
|
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 should not be relied on. There is no return value in this
|
|
|
|
case.
|
|
|
|
|
|
|
|
When invoked with *blocking* set to true, do the same thing as when called
|
|
|
|
without arguments, and return true.
|
|
|
|
|
|
|
|
When invoked with *blocking* 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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Semaphore.release()
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. _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
|
|
|
|
:meth:`set` method and reset to false with the :meth:`clear` method. The
|
|
|
|
:meth:`wait` method blocks until the flag is true.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Event()
|
|
|
|
|
|
|
|
The internal flag is initially false.
|
|
|
|
|
|
|
|
|
2008-06-13 03:32:25 -03:00
|
|
|
.. method:: Event.is_set()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Return true if and only if the internal flag is true.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Event.set()
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Event.clear()
|
|
|
|
|
|
|
|
Reset the internal flag to false. Subsequently, threads calling :meth:`wait`
|
|
|
|
will block until :meth:`set` is called to set the internal flag to true again.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Event.wait([timeout])
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
|
|
|
|
.. _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.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Timer.cancel()
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
.. _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
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
* 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).
|