I would like to download a large file when the application starts up but it should happen parallelly. As in an actual app startup, the app shouldn't wait for the file download to complete.
What I am currently doing is:
from fastapi import FastAPI
app = FastAPI()
items = {}
@app.on_event("startup")
def startup_event():
//Download file
Now this seems to work but I get a lot of critical worker timeout errors. I wanted to know if there is someway that I can do the download just when the application starts but also do it in a way that it doesn't make the application wait for the download to finish.
Lets say for example take 10GB file (https://speed.hetzner.de/10GB.bin) to download on startup.
As the application starts, it triggers an asynchronous download task using
aiohttp
, fetch a file fromhttps://speed.hetzner.de/10GB.bin
and saving it as downloaded_file.The download occurs in chunks, and this background process allows the application to initiate other tasks and respond to incoming requests without waiting for the download to complete.
Hope this block of code helps.