Код: Выделить всё
from fastapi import FastAPI
app = FastAPI()
async def async_task():
import asyncio
import time
await asyncio.sleep(1) # simulate sequential non-blocking code
time.sleep(2) # simulate sequential blocking code
await asyncio.sleep(3) # simulate sequential non-blocking code
time.sleep(4) # simulate sequential blocking code
@app.get("/")
async def run_task(tasks: BackgroundTasks):
tasks.add_task(async_task)
EDIT: Вопрос не в концепциях await/async в Python и FastAPI, поэтому я не считаю, что это дубликат. упомянутого вопроса (где объясняются концепции), но здесь я прошу помощи в том, как добиться конкретной логики реализации в отношении BackgroundTasks.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ood-with-r
Мобильная версия