再次理解asyncio/await syntax and asyncio in Python
-
Overview
asynchronous : 非同步
concurrent : 併發
coroutine : 協程
thread : 執行緒
parallelism : 並行
multiprocess : 多程式
-
asyncio/await syntax
The PEP492 is proposed to make coroutines a proper standalone concept in Python, and introduce new supporting syntax. The ultimate goal is to help establish a common, easily approachable, mental model of asynchronous programming in Python and make it as close to synchronous programming as possible.
Current Python supports implementing
coroutines
viagenerators
(PEP342
), further enhanced by theyield from
syntax introduced inPEP 380
. -
New
Coroutine Declaration
Syntaxthe following new syntax is used to declare a
native coroutine
:async def read_data(db): pass
Key properties of
coroutines
:async def
functions are always coroutines, even if they do not containawait
expressions.- It is a
SyntaxError
to haveyield
oryield from
expressions in anasync
function.
-
Await
ExpressionThe following new
await
expression is used to obtain a result of coroutine execution :async def read_data(db): data = await db.fetch("select ...") ...
await
, similarly toyield from
, suspends execution ofread_data
coroutine untildb.fetch
awaitable completes and returns the result data.《理解awaitable in Python》
It is a
SyntaxError
to useawait
outside of anasync
def function.It is a
TypeError
to pass anything other than anawaitable
object to anawait
expression. -
Asynchronous Context Managers and “
async with
”An
asynchronous context manager
is a context manager that is able to suspend execution in itsenter
andexit
methods.It is a
SyntaxError
to useasync with
outside of anasync def
function. -
asyncio - Asynchronous I/O
asyncio
is a library to write concurrent code using theasync/await
syntax.asyncio
是Python3.4
引入的標準庫,async/await
是Python3.5
引入的針對asyncio
的新語法。asyncio
的程式設計模型是一個訊息迴圈,從模型中直接獲取一個EventLoop
,然後把需要執行的攜程扔到EventLoop
中執行,實現非同步。訊息迴圈中有多個任務[a,b,c]:
- 首先執行第一個任務a,過程中遇到a內部的yield from或者await,a任務相當於continue,後續先不執行,等待await後面awaitful物件執行完成
- b任務執行,同樣的遇到await之後continue b
- a任務中await任務完成,繼續執行a過程後面的任務
- 如此繼續
不同
coroutine
是由同一個thread
執行,即asyncio
可以實現單執行緒併發IO操作。 -
Low-level APIs
Event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses.
相關文章
- 【python】非同步IO | 協程 | asyncio | await | yieldPython非同步AI
- python asyncioPython
- 【Python】asyncio框架Python框架
- python協程asyncio的個人理解Python
- 理解Python asyncio原理和簡潔使用方式Python
- Asyncio in Python and Concurrency tasksPython
- Python協程之asyncioPython
- Python asyncio 爬蟲Python爬蟲
- asyncio的基本使用框架,python高效處理資料,asyncio.gather(),asyncio. create_task(),asyncio.run(main())框架PythonAI
- 深入理解 python 非同步 i/o 庫 —— asyncioPython非同步
- Python學習筆記 - asyncioPython筆記
- python中重要的模組--asyncioPython
- Python 非同步 IO系列:認識asyncioPython非同步
- python非同步asyncio模組的使用Python非同步
- Python非同步協程(asyncio詳解)Python非同步
- asyncio(非同步io)非同步
- python3 使用 asyncio 代替執行緒Python執行緒
- 簡述python非同步i/o庫 —— asyncioPython非同步
- [python] Python非同步程式設計庫asyncio使用指北Python非同步程式設計
- 協程asyncio 模組
- python非同步程式設計之asyncio初識Python非同步程式設計
- asyncio 非同步程式設計非同步程式設計
- [python] asyncio庫常見問題與實踐案例Python
- 從頭造輪子:python3 asyncio之 sleep (4)Python
- 從頭造輪子:python3 asyncio 之 run(2)Python
- 從頭造輪子:python3 asyncio之 gather (3)Python
- asyncio非同步IO——Streams詳解非同步
- 【譯Py】Awesome Asyncio 中文版
- Python學習之路37-使用asyncio包處理併發Python
- 非同步程式設計 101: 是什麼、小試Python asyncio非同步程式設計Python
- Python311新特性-特化指令specializing adaptive interpreter-typing-asyncioPythonAPT
- 基於asyncio和redis的Python分散式任務佇列RedisPython分散式佇列
- Python非同步IO程式設計之-asyncio協程應用例子Python非同步程式設計
- 手把手教你如何使用Python的非同步IO框架:asyncio(下)Python非同步框架
- 手把手教你如何使用Python的非同步IO框架:asyncio(中)Python非同步框架
- 手把手教你如何使用Python的非同步IO框架:asyncio(上)Python非同步框架
- python——asyncio模組實現協程、非同步程式設計(三)Python非同步程式設計
- 深究Python中的asyncio庫-函式的回撥與排程Python函式