question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Most idiomatic way to run a task only once at startup?

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Nov 7, 2022

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.

1reaction
bram2000commented, Aug 19, 2021

Something like this you mean? https://fastapi.tiangolo.com/advanced/events/

Thanks, but I mentioned in the question that this is not suitable because it runs for each worker.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found