python中的同步库对应的异步库
已于 2025年09月09日 22:46 修改
访问次数:0
🗂️ Python 常用库:同步 vs 异步对照
| 领域 | 同步库(常见/传统) | 异步库(原生/推荐) |
|---|---|---|
| HTTP 客户端 | requests | aiohttp, httpx |
| Web 框架 | Flask, Django | FastAPI, aiohttp.web, Sanic |
| PostgreSQL | psycopg2 | asyncpg, databases[postgresql] |
| MySQL | mysqlclient, PyMySQL | aiomysql, databases[mysql] |
| SQLite | sqlite3 | aiosqlite |
| MongoDB | pymongo | motor |
| Redis | redis-py(新版本已支持 asyncio) | aioredis(已合并入 redis) |
| 消息队列 / Kafka | confluent-kafka-python, kafka-python | aiokafka |
| RabbitMQ | pika | aio-pika |
| 任务队列 | Celery(基于进程/线程) | dramatiq[redis, aioredis], Arq |
| Elasticsearch | elasticsearch-py | elasticsearch-py-async(社区维护,不太活跃) |
| GraphQL | graphene | ariadne, strawberry(async 支持更好) |
| WebSocket | websocket-client | websockets, starlette.websockets |
好的 ✅ 我在之前的“同步 vs 异步对照表”基础上,给你加上典型用法对比示例,这样可以更直观地理解差异。
🗂️ Python 常用库:同步 vs 异步对照 + 用法示例
1. HTTP 请求
同步(requests)
import requests
resp = requests.get("https://httpbin.org/get")
print(resp.json())
异步(aiohttp)
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
async with session.get("https://httpbin.org/get") as resp:
print(await resp.json())
asyncio.run(main())
2. PostgreSQL
同步(psycopg2)
import psycopg2
conn = psycopg2.connect("dbname=test user=postgres password=secret")
cur = conn.cursor()
cur.execute("SELECT 1;")
print(cur.fetchone())
conn.close()
异步(asyncpg)
import asyncpg
import asyncio
async def main():
conn = await asyncpg.connect("postgresql://postgres:secret@localhost/test")
row = await conn.fetchrow("SELECT 1;")
print(row)
await conn.close()
asyncio.run(main())
3. Redis
同步(redis-py)
import redis
r = redis.Redis(host="localhost", port=6379)
r.set("foo", "bar")
print(r.get("foo"))
异步(redis-py 新版支持 asyncio)
import asyncio
import redis.asyncio as redis
async def main():
r = redis.Redis(host="localhost", port=6379)
await r.set("foo", "bar")
print(await r.get("foo"))
asyncio.run(main())
4. Kafka
同步(confluent-kafka-python 消费)
from confluent_kafka import Consumer
conf = {
"bootstrap.servers": "localhost:9092",
"group.id": "mygroup",
"auto.offset.reset": "earliest"
}
consumer = Consumer(conf)
consumer.subscribe(["test_topic"])
while True:
msg = consumer.poll(1.0)
if msg is None:
continue
if msg.error():
print("Error:", msg.error())
else:
print("Message:", msg.value().decode("utf-8"))
异步(aiokafka 消费)
import asyncio
from aiokafka import AIOKafkaConsumer
async def consume():
consumer = AIOKafkaConsumer(
"test_topic",
bootstrap_servers="localhost:9092",
group_id="mygroup"
)
await consumer.start()
try:
async for msg in consumer:
print("Message:", msg.value.decode("utf-8"))
finally:
await consumer.stop()
asyncio.run(consume())
5. Web 框架
同步(Flask)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
app.run()
异步(FastAPI)
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def hello():
return {"message": "Hello, World!"}
📌 总结
- 同步库:调用简单,适合小脚本或低并发任务。
- 异步库:基于 async/await,适合高并发服务(Web API、消息处理、IO 密集型任务)。
- 最佳实践:如果要写 Web 服务,推荐全链路异步(比如 FastAPI + httpx + asyncpg + aiokafka)。
评论(0)