2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
A coroutine is a program component that allows multiple entry points of a program to suspend and resume execution at specific locations. Unlike threads and processes, the switching of coroutines is explicitly controlled by the programmer rather than scheduled by the operating system. This makes coroutines more lightweight when performing context switching, because there is no need to save and restore the entire execution environment (such as the thread stack).
In Python, coroutines are mainly implemented throughasync
andawait
Keywords. Functions defined with these keywords are called "coroutine functions". They are not executed immediately when called, but return a coroutine object. This coroutine object needs to be scheduled for execution in the event loop.
Asynchronous programming is a concurrency model that allows a program to continue executing other tasks while waiting for some long-running operations (such as I/O operations) to complete. In Python, asynchronous programming is often used with coroutines, which are scheduled for execution through an event loop.
async
andawait
async def
To define a coroutine function.await
to wait for another coroutine or asynchronous operation to complete.asyncio
The library provides an implementation of an event loop. You need to make sure your asynchronous code runs in some event loop.asyncio.run(main())
to run your main coroutine function, which automatically creates and manages the event loop.python复制代码
import asyncio | |
# 定义一个异步函数,模拟一个耗时的I/O操作 | |
async def fetch_data(url): | |
print(f"Fetching {url}...") | |
# 模拟耗时操作 | |
await asyncio.sleep(1) | |
return f"Data from {url}" | |
async def main(): | |
# 同时启动多个异步任务 | |
task1 = asyncio.create_task(fetch_data('http://example.com/data1')) | |
task2 = asyncio.create_task(fetch_data('http://example.com/data2')) | |
# 等待所有任务完成 | |
result1, result2 = await asyncio.gather(task1, task2) | |
print(result1) | |
print(result2) | |
# 运行主协程 | |
asyncio.run(main()) |
In this example,fetch_data
Is an asynchronous function that simulates a network request.main
In the function, we start twofetch_data
Asynchronous tasks, and useawait asyncio.gather()
Wait for them to finish at the same time.
asyncio
It is part of the Python standard library and is used to support coroutines and asynchronous programming. It also provides a rich API, such asasyncio.Queue
、asyncio.Lock
etc., for handling concurrency and synchronization issues in asynchronous code.