faststream python 快速event 处理框架
faststream python 快速event 处理框架,asyncapi 文档生成
包含的特性
- 多broker 支持
- 基于pydantic 的数据校验
- async api 文档生成
- 依赖注入支持
- 强大的扩展能力
- 不少框架的集成支持(fastapi web 框架)
- 支持自动代码生成
参考使用
基于了redis,集成了fastapi
- app.py
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from faststream.redis.fastapi import RedisRouter, Logger
router = RedisRouter("redis://localhost:6379")
class Incoming(BaseModel):
m: dict
def call():
return True
@router.subscriber("test")
@router.publisher("response")
async def hello(m: Incoming, logger: Logger, d=Depends(call)):
logger.info(m)
return m
@router.subscriber("response")
async def appdemo(m: Incoming,logger: Logger):
logger.info(m)
return {"response": "Hello, response!"}
@router.get("/")
async def hello_http():
return "Hello, HTTP!"
@router.post("/send")
async def hello_http(msg: Incoming):
await router.broker.publish(msg,"test")
return "Hello, HTTP!"
app = FastAPI(lifespan=router.lifespan_context)
app.include_router(router)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
说明
faststream 使用简单,集成能力强大,对于开发消息事件驱动的应用还是值得试用下的
参考资料
https://github.com/airtai/faststream
https://faststream.airt.ai/latest/