Commit b96a3545 authored by Antoine Pitrou's avatar Antoine Pitrou

Small improvements to the threading docs: better publicize support for the with statement.

parent 2c9f1042
...@@ -389,6 +389,8 @@ called in the locked state; it changes the state to unlocked and returns ...@@ -389,6 +389,8 @@ 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 immediately. If an attempt is made to release an unlocked lock, a
:exc:`RuntimeError` will be raised. :exc:`RuntimeError` will be raised.
Locks also support the :ref:`context manager protocol <with-locks>`.
When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the 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` 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 call resets the state to unlocked; which one of the waiting threads proceeds
...@@ -429,7 +431,8 @@ All methods are executed atomically. ...@@ -429,7 +431,8 @@ All methods are executed atomically.
.. method:: Lock.release() .. method:: Lock.release()
Release a lock. 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 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 are blocked waiting for the lock to become unlocked, allow exactly one of them
...@@ -458,6 +461,8 @@ call pairs may be nested; only the final :meth:`~Lock.release` (the ...@@ -458,6 +461,8 @@ 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 :meth:`~Lock.release` of the outermost pair) resets the lock to unlocked and
allows another thread blocked in :meth:`~Lock.acquire` to proceed. allows another thread blocked in :meth:`~Lock.acquire` to proceed.
Reentrant locks also support the :ref:`context manager protocol <with-locks>`.
.. method:: RLock.acquire(blocking=True, timeout=-1) .. method:: RLock.acquire(blocking=True, timeout=-1)
...@@ -512,10 +517,11 @@ passed in or one will be created by default. Passing one in is useful when ...@@ -512,10 +517,11 @@ 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 several condition variables must share the same lock. The lock is part of
the condition object: you don't have to track it separately. the condition object: you don't have to track it separately.
A condition variable obeys the :term:`context manager` protocol: using the A condition variable obeys the :ref:`context manager protocol <with-locks>`:
``with`` statement acquires the associated lock for the duration of the using the ``with`` statement acquires the associated lock for the duration of
enclosed block. The :meth:`~Condition.acquire` and :meth:`~Condition.release` the enclosed block. The :meth:`~Condition.acquire` and
methods also call the corresponding methods of the associated lock. :meth:`~Condition.release` methods also call the corresponding methods of
the associated lock.
Other methods must be called with the associated lock held. The Other methods must be called with the associated lock held. The
:meth:`~Condition.wait` method releases the lock, and then blocks until :meth:`~Condition.wait` method releases the lock, and then blocks until
...@@ -686,6 +692,8 @@ call. The counter can never go below zero; when :meth:`~Semaphore.acquire` ...@@ -686,6 +692,8 @@ 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 finds that it is zero, it blocks, waiting until some other thread calls
:meth:`~Semaphore.release`. :meth:`~Semaphore.release`.
Semaphores also support the :ref:`context manager protocol <with-locks>`.
.. class:: Semaphore(value=1) .. class:: Semaphore(value=1)
...@@ -742,11 +750,12 @@ main thread would initialize the semaphore:: ...@@ -742,11 +750,12 @@ main thread would initialize the semaphore::
Once spawned, worker threads call the semaphore's acquire and release methods Once spawned, worker threads call the semaphore's acquire and release methods
when they need to connect to the server:: when they need to connect to the server::
pool_sema.acquire() with pool_sema:
conn = connectdb() conn = connectdb()
... use connection ... try:
conn.close() ... use connection ...
pool_sema.release() finally:
conn.close()
The use of a bounded semaphore reduces the chance that a programming error which 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. causes the semaphore to be released more than it's acquired will go undetected.
...@@ -947,19 +956,24 @@ Using locks, conditions, and semaphores in the :keyword:`with` statement ...@@ -947,19 +956,24 @@ Using locks, conditions, and semaphores in the :keyword:`with` statement
All of the objects provided by this module that have :meth:`acquire` and 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` :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, statement. The :meth:`acquire` method will be called when the block is
and :meth:`release` will be called when the block is exited. entered, and :meth:`release` will be called when the block is exited. Hence,
the following snippet::
Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`, with some_lock:
:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as # do something...
:keyword:`with` statement context managers. For example::
import threading is equivalent to::
some_rlock = threading.RLock() some_lock.acquire()
try:
# do something...
finally:
some_lock.release()
with some_rlock: Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
print("some_rlock is locked while this executes") :class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
:keyword:`with` statement context managers.
.. _threaded-imports: .. _threaded-imports:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment