异步编程是一种非阻塞的编程范式,允许程序在等待I/O操作(如网络请求、文件读写)完成时执行其他任务,而不是干等。
事件循环是asyncio的核心,负责调度和执行异步任务。它不断检查并执行以下操作:
协程是asyncio的基本执行单元,使用async def
定义的函数:
async def my_coroutine():
await asyncio.sleep(1)
return "Done"
import asyncio
async def hello_world():
print("Hello")
await asyncio.sleep(1)
print("World")
# Python 3.7+ 推荐方式
asyncio.run(hello_world())
# 旧版本方式
# loop = asyncio.get_event_loop()
# loop.run_until_complete(hello_world())
# loop.close()
async def main():
task1 = asyncio.create_task(hello_world())
task2 = asyncio.create_task(hello_world())
await task1
await task2
asyncio.run(main())
async def fetch_data(delay, id):
print(f"Fetching data {id}...")
await asyncio.sleep(delay)
print(f"Data {id} fetched")
return {"id": id, "delay": delay}
async def main():
results = await asyncio.gather(
fetch_data(2, 1),
fetch_data(1, 2),
fetch_data(3, 3),
)
print(results)
asyncio.run(main())
loop = asyncio.get_event_loop()
loop.run_until_complete(coro)
loop.run_forever()
loop.stop()
await asyncio.sleep(1) # 休眠1秒
results = await asyncio.gather(
coro1(),
coro2(),
return_exceptions=True # 异常时返回异常对象而不是抛出
)
done, pending = await asyncio.wait(
[coro1(), coro2()],
timeout=2,
return_when=asyncio.FIRST_COMPLETED
)
task = asyncio.create_task(coro())
await asyncio.shield(task) # 防止任务被取消
task = asyncio.create_task(coro())
task.cancel()
try:
await task
except asyncio.CancelledError:
print("Task was cancelled")
try:
await asyncio.wait_for(coro(), timeout=1.0)
except asyncio.TimeoutError:
print("Timeout!")
class AsyncResource:
async def __aenter__(self):
print("Opening resource")
await asyncio.sleep(1)
return self
async def __aexit__(self, exc_type, exc, tb):
print("Closing resource")
await asyncio.sleep(1)
async def main():
async with AsyncResource() as resource:
print("Using resource")
asyncio.run(main())
class AsyncCounter:
def __init__(self, stop):
self.current = 0
self.stop = stop
def __aiter__(self):
return self
async def __anext__(self):
if self.current < self.stop:
await asyncio.sleep(1)
self.current += 1
return self.current
else:
raise StopAsyncIteration
async def main():
async for i in AsyncCounter(3):
print(i)
asyncio.run(main())
async def async_gen():
for i in range(3):
await asyncio.sleep(1)
yield i
async def main():
async for item in async_gen():
print(item)
asyncio.run(main())
import time
def blocking_io():
time.sleep(1)
return "Blocking IO done"
async def main():
loop = asyncio.get_running_loop()
# 1. 在默认线程池中运行
result = await loop.run_in_executor(
None, blocking_io
)
print(result)
# 2. 在自定义线程池中运行
# with concurrent.futures.ThreadPoolExecutor() as pool:
# result = await loop.run_in_executor(
# pool, blocking_io
# )
# print(result)
asyncio.run(main())
def sync_main():
return asyncio.run(async_main())
async def async_main():
await asyncio.sleep(1)
return "Done"
print(sync_main())
import uvloop
uvloop.install()
import aiohttp
async def fetch_url(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
tasks = [
fetch_url(session, "http://example.com") for _ in range(5)
]
results = await asyncio.gather(*tasks)
print(f"Fetched {len(results)} pages")
asyncio.run(main())
import asyncpg
async def fetch_data():
conn = await asyncpg.connect(
user='user', password='pass',
database='db', host='localhost')
try:
result = await conn.fetch('SELECT * FROM table')
return result
finally:
await conn.close()
asyncio.run(fetch_data())
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/")
async def read_root():
await asyncio.sleep(1)
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}