FastAPI中使用调度程序执行异步任务,与Redis交互
FastAPI中使用调度程序执行异步任务,与Redis交互
pip install fastapi[all]
pip install aioredisimport asyncio
async def task():
# 这里是你的任务逻辑
# 例如,与Redis进行交互
redis = await aioredis.create_redis_pool('redis://localhost')
# 执行Redis操作
await redis.set('key', 'value')
value = await redis.get('key')
print(value)
redis.close()
await redis.wait_closed()
# 创建一个调度程序,并设置任务执行的时间间隔
async def scheduler():
while True:
await task()
await asyncio.sleep(60) # 每60秒执行一次任务
Last updated