Dependency defined on startup event
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
@app.on_event("startup")
async def startup_event():
something = await func()
myobj = MyObj(something)
###
@router.get("")
async def index():
# need myobj instance here
Description
I need to execute some async initialization on startup creating an instance of myobj
, and then I need this instance as a dependency in routers endpoint.
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.68.1
Python Version
3.8.8
Additional Context
I need to create an instance of a “master” object asynchronously during the startup of FastApi, and then to get this exact instance in routers endpoint, but I cannot figure out how to pass it as a dependency.
(I’m aware that if I have multiple workers each worker will get a different “master” object but that is ok, the main point here is that within a single worker an instance is created after async operation on startup, and then reused in routers endpoint.)
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
What is a Dependency? Dependency Definition and Examples
A dependency describes the relationship among activities and specifies the particular order in which they need to be performed.
Read more >Events: startup - shutdown - FastAPI
You can define event handlers (functions) that need to be executed before the application starts up, or when the application is shutting down....
Read more >Dependency injection in ASP.NET Core | Microsoft Learn
Learn how ASP.NET Core implements dependency injection and how to use it.
Read more >What is a Task Dependency? Definition, Types, and Examples
A task dependency is a relationship that requires a particular order for tasks to be performed. It means that one preceding task relies...
Read more >Everything You Need to Know About Task Dependencies
External: this describes an input from an external source that is required before a task can proceed. These set dependencies frequently take the ......
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
Another alternative is to store it in the app state:
Or, more in line with traditional dependency injection if you imagine that
dependency_overrides
is a “container”:If this was a more realistic situation, I’d hope this looked like:
@adriangb thank you so much for the idea about using dependency overrides. It really helped me out!