Most idiomatic way to run a task only once at startup?
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI
app = FastAPI()
async def download_data_files():
# download some files from s3 using aiohttp
# post process the files
pass
@app.get("/data")
def data():
# return some data that relies on the downloaded files
return "something"
Description
I think my question is simple, but having done a lot of searching GitHub and google I’m starting to wonder if I’m trying to bend FastAPI in a way it doesn’t like!
What is the most idiomatic way of running a one-off task, on startup?
In my simple example above, I want to run download_data_files
once and only once, before serving any requests.
I’ve tried using the startup
event but this executes in every worker which is wasteful (I end up downloading the same files n
times).
I’ve tried to use the create_app
pattern along with gunicorn --preload
, but I couldn’t get it to work, and from what I’ve read it doesn’t sound like a recommended approach anyway.
Operating System
Linux
Operating System Details
N/A
FastAPI Version
0.68.0
Python Version
Python 3.8.7
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Schedule task to run at startup only once (next boot)
I would like to be able to add a scheduled task to run a script/program at next computer startup and only run once....
Read more >3 Ways to Run Code Once at Application Startup in ASP.NET ...
Run the Code Directly from Program or Startup Classes. Probably the most obvious and straightforward way to execute a piece of code only...
Read more >Idiomatic place for this code when the application boots?
Application.start/2 : Once the supervisor has been launched you can log before returning {:ok, pid} to the caller.
Read more >How to execute a piece of code only once? - c++
I have an application which has several functions in it. Each function can be called many times based on user input ...
Read more >How To Use Go with MongoDB Using the ...
Each Task is then appended to the slice of tasks created at the start of the function. Once the Cursor is exhausted, it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The best way to for it would be to run the script before starting the workers. How you run it depends on how you deploy.
Then you could reuse that script/function inside the path operation to update the files.
Thanks, but I mentioned in the question that this is not suitable because it runs for each worker.