Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b96a354

Browse files
committed
Small improvements to the threading docs: better publicize support for the with statement.
1 parent 2c9f104 commit b96a354

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

Doc/library/threading.rst

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ called in the locked state; it changes the state to unlocked and returns
389389
immediately. If an attempt is made to release an unlocked lock, a
390390
:exc:`RuntimeError` will be raised.
391391

392+
Locks also support the :ref:`context manager protocol <with-locks>`.
393+
392394
When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the
393395
state to turn to unlocked, only one thread proceeds when a :meth:`~Lock.release`
394396
call resets the state to unlocked; which one of the waiting threads proceeds
@@ -429,7 +431,8 @@ All methods are executed atomically.
429431

430432
.. method:: Lock.release()
431433

432-
Release a lock.
434+
Release a lock. This can be called from any thread, not only the thread
435+
which has acquired the lock.
433436

434437
When the lock is locked, reset it to unlocked, and return. If any other threads
435438
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
458461
:meth:`~Lock.release` of the outermost pair) resets the lock to unlocked and
459462
allows another thread blocked in :meth:`~Lock.acquire` to proceed.
460463

464+
Reentrant locks also support the :ref:`context manager protocol <with-locks>`.
465+
461466

462467
.. method:: RLock.acquire(blocking=True, timeout=-1)
463468

@@ -512,10 +517,11 @@ passed in or one will be created by default. Passing one in is useful when
512517
several condition variables must share the same lock. The lock is part of
513518
the condition object: you don't have to track it separately.
514519

515-
A condition variable obeys the :term:`context manager` protocol: using the
516-
``with`` statement acquires the associated lock for the duration of the
517-
enclosed block. The :meth:`~Condition.acquire` and :meth:`~Condition.release`
518-
methods also call the corresponding methods of the associated lock.
520+
A condition variable obeys the :ref:`context manager protocol <with-locks>`:
521+
using the ``with`` statement acquires the associated lock for the duration of
522+
the enclosed block. The :meth:`~Condition.acquire` and
523+
:meth:`~Condition.release` methods also call the corresponding methods of
524+
the associated lock.
519525

520526
Other methods must be called with the associated lock held. The
521527
: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`
686692
finds that it is zero, it blocks, waiting until some other thread calls
687693
:meth:`~Semaphore.release`.
688694

695+
Semaphores also support the :ref:`context manager protocol <with-locks>`.
696+
689697

690698
.. class:: Semaphore(value=1)
691699

@@ -742,11 +750,12 @@ main thread would initialize the semaphore::
742750
Once spawned, worker threads call the semaphore's acquire and release methods
743751
when they need to connect to the server::
744752

745-
pool_sema.acquire()
746-
conn = connectdb()
747-
... use connection ...
748-
conn.close()
749-
pool_sema.release()
753+
with pool_sema:
754+
conn = connectdb()
755+
try:
756+
... use connection ...
757+
finally:
758+
conn.close()
750759

751760
The use of a bounded semaphore reduces the chance that a programming error which
752761
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
947956

948957
All of the objects provided by this module that have :meth:`acquire` and
949958
:meth:`release` methods can be used as context managers for a :keyword:`with`
950-
statement. The :meth:`acquire` method will be called when the block is entered,
951-
and :meth:`release` will be called when the block is exited.
959+
statement. The :meth:`acquire` method will be called when the block is
960+
entered, and :meth:`release` will be called when the block is exited. Hence,
961+
the following snippet::
952962

953-
Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
954-
:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
955-
:keyword:`with` statement context managers. For example::
963+
with some_lock:
964+
# do something...
956965

957-
import threading
966+
is equivalent to::
958967

959-
some_rlock = threading.RLock()
968+
some_lock.acquire()
969+
try:
970+
# do something...
971+
finally:
972+
some_lock.release()
960973

961-
with some_rlock:
962-
print("some_rlock is locked while this executes")
974+
Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`,
975+
:class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as
976+
:keyword:`with` statement context managers.
963977

964978

965979
.. _threaded-imports:

0 commit comments

Comments
 (0)