欢迎来到程序员中文网!

首页 Linux Mysql C++ Python PHP JavaScript 资源下载 动态 开源推荐
我要投稿 投诉建议

Python 异步编程 asyncio 与 aiohttp 实战

时间:2026年08月12日 05:40:34 浏览:1

Python 异步编程 asyncio 与 aiohttp 实战


asyncio 是 Python 异步 I/O 框架,适合 IO 密集型任务。


1. 基本用法


import asyncio

async def say_hello():
print("Hello")
await asyncio.sleep(1)
print("World")

asyncio.run(say_hello())

2. 并发任务


async def main():
tasks = [asyncio.create_task(say_hello()) for _ in range(3)]
await asyncio.gather(*tasks)

3. aiohttp 异步 HTTP 请求


import aiohttp

async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
async with aiohttp.ClientSession() as session:
results = await asyncio.gather(
fetch(session, "http://httpbin.org/get"),
fetch(session, "http://httpbin.org/ip")
)
print(results)

4. 并发控制(Semaphore)


sem = asyncio.Semaphore(10)

async def bounded_fetch(session, url):
async with sem:
return await fetch(session, url)

异步爬虫比多线程更轻量,可大幅提升 IO 密集型应用性能。