Home > Back-end >  Running function in FastAPI view while returning an HTTP response without waiting for the function t
Running function in FastAPI view while returning an HTTP response without waiting for the function t

Time:10-15

I have the following code:

from fastapi import FastAPI, Request, Form
import uvicorn
from testphoto.utils.logger import get_log
import datetime
import time
import asyncio

log = get_log()

app = FastAPI()

def process():
    log.info("Sleeping at " str(datetime.datetime.now()))
    time.sleep(5)
    log.info("Woke up at " str(datetime.datetime.now()))
    return "Sucess"

@app.post("/api/photos")
async def root(request: Request, photo: str = Form()):
    process()
    return {"message": "Hello World"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8008)

What I want to do is to run the function process and return the response while keeping the function process running. I've read some documentation about asyncio and FastAPI but I'm still unable to figure this out. Where would you point me to in order make the code do exactly as I want?

CodePudding user response:

What you're looking for, if not CPU intensive, is called a background task.

You can read more on the docs

https://fastapi.tiangolo.com/tutorial/background-tasks/

  • Related