Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 0

source-code-analysis/python3.8.1

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
python3.8.1
/
Doc
/
library
/
asyncio-sync.rst
python3.8.1
/
Doc
/
library
/
asyncio-sync.rst
asyncio-sync.rst 8.88 KB
Copy Edit Raw Blame History
zhangweibo authored 2021年11月16日 09:46 +08:00 . git init
.. currentmodule:: asyncio

Synchronization Primitives

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


asyncio synchronization primitives are designed to be similar to those of the :mod:`threading` module with two important caveats:

  • asyncio primitives are not thread-safe, therefore they should not be used for OS thread synchronization (use :mod:`threading` for that);
  • methods of these synchronization primitives do not accept the timeout argument; use the :func:`asyncio.wait_for` function to perform operations with timeouts.

asyncio has the following basic synchronization primitives:


Lock

Implements a mutex lock for asyncio tasks. Not thread-safe.

An asyncio lock can be used to guarantee exclusive access to a shared resource.

The preferred way to use a Lock is an :keyword:`async with` statement:

lock = asyncio.Lock()

# ... later
async with lock:
 # access shared state

which is equivalent to:

lock = asyncio.Lock()

# ... later
await lock.acquire()
try:
 # access shared state
finally:
 lock.release()
.. deprecated-removed:: 3.8 3.10
 The *loop* parameter.

.. coroutinemethod:: acquire()

 Acquire the lock.

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

 When more than one coroutine is blocked in :meth:`acquire`
 waiting for the lock to be unlocked, only one coroutine
 eventually proceeds.

 Acquiring a lock is *fair*: the coroutine that proceeds will be
 the first coroutine that started waiting on the lock.

.. method:: release()

 Release the lock.

 When the lock is *locked*, reset it to *unlocked* and return.

 If the lock is *unlocked*, a :exc:`RuntimeError` is raised.

.. method:: locked()

 Return ``True`` if the lock is *locked*.

Event

An event object. Not thread-safe.

An asyncio event can be used to notify multiple asyncio tasks that some event has happened.

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 set to true. The flag is set to false initially.

.. deprecated-removed:: 3.8 3.10
 The *loop* parameter.

Example:

async def waiter(event):
 print('waiting for it ...')
 await event.wait()
 print('... got it!')

async def main():
 # Create an Event object.
 event = asyncio.Event()

 # Spawn a Task to wait until 'event' is set.
 waiter_task = asyncio.create_task(waiter(event))

 # Sleep for 1 second and set the event.
 await asyncio.sleep(1)
 event.set()

 # Wait until the waiter task is finished.
 await waiter_task

asyncio.run(main())
.. coroutinemethod:: wait()

 Wait until the event is set.

 If the event is set, return ``True`` immediately.
 Otherwise block until another task calls :meth:`set`.

.. method:: set()

 Set the event.

 All tasks waiting for event to be set will be immediately
 awakened.

.. method:: clear()

 Clear (unset) the event.

 Tasks awaiting on :meth:`wait` will now block until the
 :meth:`set` method is called again.

.. method:: is_set()

 Return ``True`` if the event is set.

Condition

Semaphore

BoundedSemaphore


.. deprecated:: 3.7

 Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
 :keyword:`with` statement (``with await lock``, ``with (yield from
 lock)``) is deprecated. Use ``async with lock`` instead.
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /