I'm trying to add background tasks to my middleware, but haven't found any official way to do it in their docs, this is what I have tried so far:
async def task1():
logging.info("Waiting ...")
time.sleep(5)
logging.info("Waited.")
@app.middleware("http")
async def add_process_time_header(request, call_next, bt=Depends(BackgroundTasks)):
a = bt.dependency()
a.add_task(task1)
a()
return await call_next(request)
This is blocking my requests, also shouldn't I be able to call async functions without await? I want to completely ignore the results of the background tasks, I dont need results from bg tasks
CodePudding user response:
I found some way to solve your problem, i think you can find better way,but for now you can use like this:
import logging
import time
import asyncio
from fastapi import FastAPI, Request
app = FastAPI()
@app.get("/")
async def call_api():
await asyncio.sleep(5)
return True
async def task1():
logging.info("Waiting ...")
await asyncio.sleep(5)
logging.info("Waited.")
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response , res__task1 = await asyncio.gather(*[call_next(request),task1()])
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
print(process_time)
return response
A total of 5 seconds are spent on all tasks
CodePudding user response:
You are using a blocking function for your background task.
time.sleep(5)
Eventhough you are using an async fuction, it doesn't contain any non-blocking i/o. So in effect, it cannot be executed asynchronously.
You should use the following sleep function for a non-blocking timeout.
import asyncio
await asyncio.sleep(5)
In this way, your sleep
will not block requests being processed.
Read more here:Python 3.7 - asyncio.sleep() and time.sleep()