文章目录
- async def fetch_data(url): # 模拟网络请求 await asyncio.sleep(1) return f”Data from {url}”
- async def main(): result = await fetch_data(“https://example.com”) print(result) # Python 3.7+ 推荐方式 asyncio.run(main())
- 使用 asyncio.gather() 或 asyncio.create_task(): async def main(): # 同时执行多个协程 task1 = asyncio.create_task(fetch_data(“url1”)) task2 = asyncio.create_task(fetch_data(“url2”)) # 等待所有任务完成 results = await asyncio.gather(task1, task2) print(results)
- 使用信号量(Semaphore)限制同时运行的任务数: async def limited_fetch(url, semaphore): async with semaphore: return await fetch_data(url) async def main(): semaphore = asyncio.Semaphore(5) # 最大并发5 tasks = [limited_fetch(url, semaphore) for url in urls] await asyncio.gather(*tasks)
- async def fetch_with_timeout(): try: async with asyncio.timeout(3): # Python 3.11+ await fetch_data(“slow_url”) except TimeoutError: print(“Timeout!”)
- async def main(): loop = asyncio.get_running_loop() future = loop.create_future() def callback(): future.set_result(“Done”) loop.call_soon(callback) result = await future print(result)
目录
- 一、核心概念
- 二、使用场景
- 三、基本用法
- 1. 定义协程
- 2. 运行协程
- 3. 并发执行多个任务
- 四、关键 API 详解
- 五、高级用法
- 1. 控制并发量
- 2. 超时控制
- 3. 回调与 Future
- 六、常见错误
- 七、完整示例
- 八、总结
在 Python 中,await 和 asyncio 是异步编程的核心工具,用于高效处理 I/O 密集型任务(如网络请求、文件读写、数据库操作等)。它们的核心思想是通过 协程(Coroutine) 和 事件循环(Event Loop) 实现非阻塞并发,避免线程切换的开销。
协程(Coroutine)
用 async def 定义的函数,返回一个协程对象,可以通过 await 挂起执行,让出控制权。
async def my_coroutine():
await asyncio.sleep(1)
print("Done")
事件循环(Event Loop)
异步程序的核心调度器,负责执行协程并在 I/O 操作时切换任务。
可等待对象(Awaitable)
包括协程、asyncio.Task 和 asyncio.Future。只有可等待对象才能被 await。
- 高并发网络请求
如爬虫、API 调用等需要同时处理大量连接的场景。
- Web 服务器
如 FastAPI、Sanic 等异步框架处理 HTTP 请求。
- 数据库操作
异步驱动(如 asyncpg、aiomysql)避免阻塞主线程。
- 实时通信
WebSocket、聊天服务器等需要长连接的场景。
如爬虫、API 调用等需要同时处理大量连接的场景。
如 FastAPI、Sanic 等异步框架处理 HTTP 请求。
异步驱动(如
asyncpg、aiomysql)避免阻塞主线程。WebSocket、聊天服务器等需要长连接的场景。
async def fetch_data(url):
# 模拟网络请求
await asyncio.sleep(1)
return f"Data from {url}"
async def main():
result = await fetch_data("https://example.com")
print(result)
# Python 3.7+ 推荐方式
asyncio.run(main())
使用 asyncio.gather() 或 asyncio.create_task():
async def main():
# 同时执行多个协程
task1 = asyncio.create_task(fetch_data("url1"))
task2 = asyncio.create_task(fetch_data("url2"))
# 等待所有任务完成
results = await asyncio.gather(task1, task2)
print(results)
asyncio.run(coro)
- 启动事件循环并运行协程(Python 3.7+)。
asyncio.create_task(coro)
- 将协程包装为
Task,加入事件循环并发执行。
asyncio.gather(*coros)
- 并发执行多个协程,返回结果列表。
asyncio.sleep(delay)
- 非阻塞等待(模拟 I/O 操作)。
asyncio.run(coro)
- 启动事件循环并运行协程(Python 3.7+)。
asyncio.create_task(coro)
- 将协程包装为
Task,加入事件循环并发执行。
asyncio.gather(*coros)
- 并发执行多个协程,返回结果列表。
asyncio.sleep(delay)
- 非阻塞等待(模拟 I/O 操作)。
使用信号量(Semaphore)限制同时运行的任务数:
async def limited_fetch(url, semaphore):
async with semaphore:
return await fetch_data(url)
async def main():
semaphore = asyncio.Semaphore(5) # 最大并发5
tasks = [limited_fetch(url, semaphore) for url in urls]
await asyncio.gather(*tasks)
async def fetch_with_timeout():
try:
async with asyncio.timeout(3): # Python 3.11+
await fetch_data("slow_url")
except TimeoutError:
print("Timeout!")
async def main():
loop = asyncio.get_running_loop()
future = loop.create_future()
def callback():
future.set_result("Done")
loop.call_soon(callback)
result = await future
print(result)
忘记 await
协程不会被自动执行:
async def main():
fetch_data("url") # 错误!没有 await
阻塞主线程
在协程中调用同步代码(如 time.sleep())会阻塞事件循环:
async def bad_example():
time.sleep(1) # 错误!应使用 await asyncio.sleep(1)
滥用并发
异步不适合 CPU 密集型任务,此时应使用多进程。
import asyncio
async def download(url, delay):
print(f"Start downloading {url}")
await asyncio.sleep(delay)
print(f"Finished {url}")
return url
async def main():
urls = [
("https://site1.com", 1),
("https://site2.com", 2),
("https://site3.com", 3),
]
tasks = [asyncio.create_task(download(url, delay)) for url, delay in urls]
results = await asyncio.gather(*tasks)
print("All done:", results)
if __name__ == "__main__":
asyncio.run(main())
输出:
Start downloading https://site1.com
Start downloading https://site2.com
Start downloading https://site3.com
Finished https://site1.com
Finished https://site2.com
Finished https://site3.com
All done: ['https://site1.com', 'https://site2.com', 'https://site3.com']
- 适用场景:I/O 密集型任务,如网络、文件、数据库操作。
- 关键点:
- 使用
async def 定义协程,用 await 挂起阻塞操作。
- 通过
asyncio.create_task() 和 asyncio.gather() 实现并发。
- 避免在协程中调用阻塞同步代码。
- 使用
async def定义协程,用await挂起阻塞操作。 - 通过
asyncio.create_task()和asyncio.gather()实现并发。 - 避免在协程中调用阻塞同步代码。
通过合理使用 asyncio,可以在单线程内高效处理成千上万的并发连接。
到此这篇关于Python异步编程之await与asyncio基本用法详解的文章就介绍到这了,更多相关Python await与asyncio内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python中的异步:async 和 await以及操作中的事件循环、回调和异常
- 在python里使用await关键字来等另外一个协程的实例
- Python从使用线程到使用async/await的深入讲解
- python中利用await关键字如何等待Future对象完成详解
- python协程异步IO中asyncio的使用
- python协程库asyncio(异步io)问题
- Python asyncio的基本使用示例教程