📅  最后修改于: 2023-12-03 14:45:56.103000             🧑  作者: Mango
Asynchronous programming is an important paradigm that every programmer should know. In this article, we will introduce you to the concept of async
and await
in Python.
In synchronous programming, a program waits for a task to finish before moving on to the next one. In contrast, asynchronous programming allows a program to move on to the next task before the previous one has finished executing.
async
and await
Keywords in PythonPython introduced the async
and await
keywords in version 3.5 as a way to write more concise and readable code for asynchronous programming.
async
KeywordThe async
keyword is used to define a coroutine function. A coroutine function is a special type of function that can be paused and resumed. When a coroutine function is called, it returns a coroutine object, which can be used to execute the coroutine.
Here is an example of a simple coroutine function:
async def my_coroutine():
print('Start')
await asyncio.sleep(1)
print('End')
In this example, the asyncio.sleep()
function is used to introduce a delay of one second. When the coroutine function is called, the first print statement is executed, and then the coroutine is paused for one second. After that, the second print statement is executed.
await
KeywordThe await
keyword is used to pause a coroutine until a result is available. When a coroutine function encounters an await
expression, it pauses its execution and waits for the result. The result can be any object with a __await__()
method or a coroutine.
Here is an example of using await
to pause a coroutine until a result is available:
async def my_coroutine():
print('Start')
result = await my_function()
print(result)
In this example, the await
keyword is used to pause the execution of the coroutine until the result of my_function()
is available.
In this article, we have introduced you to the concept of async
and await
in Python. We hope that this article has helped you understand asynchronous programming in Python better.
Markdown:
Asynchronous programming is an important paradigm that every programmer should know. In this article, we will introduce you to the concept of async
and await
in Python.
In synchronous programming, a program waits for a task to finish before moving on to the next one. In contrast, asynchronous programming allows a program to move on to the next task before the previous one has finished executing.
async
and await
Keywords in PythonPython introduced the async
and await
keywords in version 3.5 as a way to write more concise and readable code for asynchronous programming.
async
KeywordThe async
keyword is used to define a coroutine function. A coroutine function is a special type of function that can be paused and resumed. When a coroutine function is called, it returns a coroutine object, which can be used to execute the coroutine.
Here is an example of a simple coroutine function:
async def my_coroutine():
print('Start')
await asyncio.sleep(1)
print('End')
await
KeywordThe await
keyword is used to pause a coroutine until a result is available. When a coroutine function encounters an await
expression, it pauses its execution and waits for the result. The result can be any object with a __await__()
method or a coroutine.
Here is an example of using await
to pause a coroutine until a result is available:
async def my_coroutine():
print('Start')
result = await my_function()
print(result)
In this article, we have introduced you to the concept of async
and await
in Python. We hope that this article has helped you understand asynchronous programming in Python better.