Technology Sharing

What are coroutines in Python and how to implement asynchronous programming in Python

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Coroutine

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 throughasyncandawaitKeywords. 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

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.

How to implement asynchronous programming in Python

1. Useasyncandawait
  • Defining coroutine functions:useasync defTo define a coroutine function.
  • Waiting for coroutines: Inside the coroutine function, you can useawaitto wait for another coroutine or asynchronous operation to complete.
2. Use an event loop
  • PythonasyncioThe library provides an implementation of an event loop. You need to make sure your asynchronous code runs in some event loop.
  • useasyncio.run(main())to run your main coroutine function, which automatically creates and manages the event loop.
3. Sample Code
 

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_dataIs an asynchronous function that simulates a network request.mainIn the function, we start twofetch_dataAsynchronous tasks, and useawait asyncio.gather()Wait for them to finish at the same time.

4. Notes
  • Asynchronous programming and coroutines make it easier to deal with concurrency, but also introduce new complexities such as asynchronous context management, error handling, etc.
  • When designing asynchronous code, pay special attention to which operations are blocking (for example, synchronous I/O operations) and use asynchronous versions of libraries and APIs whenever possible.
  • asyncioIt is part of the Python standard library and is used to support coroutines and asynchronous programming. It also provides a rich API, such asasyncio.Queueasyncio.Locketc., for handling concurrency and synchronization issues in asynchronous code.