Helper tool for Application/Singleton Scoped Dependencies #9215
Replies: 2 comments 1 reply
-
Hi! I have similar problem when start using Celery with FastAPI. It's very difficult to reuse the same concept/solution for dependency injection in celery task and provide consistency in all code base. I wrote my own implementation of how resolve and inject dependencies for task but will be happy if FastAPI provide ability to reuse it and will not need to create my own bike. My example of code for celery task:
FastAPI provide great developer experience when you develop web API but when the application starts grow and need to integrate with other tools/libraries it's difficult to keep the same quality of architecture for whole project. |
Beta Was this translation helpful? Give feedback.
-
@mike-optifino Could you please share how your example exactly works? I am trying to accomplish exactly that. |
Beta Was this translation helpful? Give feedback.
-
Hi all, I wanted to share a project I started working on which can be integrated into FastAPI applications.
I wouldn't consider this project production ready yet, but was hoping to get some feedback from the FastAPI community.
A lot of this work was started because I wanted a way to easily create singleton or application scoped dependencies in my FastAPI application and not define them as module level globals. This was to allow easily mocking components during testing, and avoid having module loads create stateful items. Examples of where I wanted this were database connection-pools and Kafka producers.
I also wanted to reuse my FastAPI codebase in my Kafka consumer applications while still being able to use dependency injection. This is not easily done outside the context of a webrequest, which my Kafka consumer applications do not have access to.
So I started a library that I'm calling Flex DI to perform dependency injection in a similar way to FastAPI, in that dependencies can be defined as easy to understand provider functions, while supporting a concept of scopes (application vs request scoped).
Gluing it back together with FastAPI was actually quite painless. This example shows how to create a SQLAlchemy engine object as an application scoped dependency without having it defined as a global in the module. Everything that requires request state can continue using the FastAPI DI system, but more generic objects can be offloaded to Flex DI, keeping them more easily used in separate non web-based applications.
The integration with FastAPI is fairly basic at this point, but seems to act as I want it to. Here's the implementation. I tie the startup of the dependency injection system to the startup and shutdown events of FastAPI, and within that startup create an application scope for dependency injection.
Each time a new request comes in, a request scope is created and attached to the request object, which allows resolving dependencies defined as
FlexDepends
. TheFlexDepends
is a simple factory function for creatingfastapi.Depends
instances, configured to fetch objects from Flex DI.I would love to hear what people think, if this could be improved upon in terms of usability, or any other general concerns.
P.S. Thank you @tiangolo for creating such an amazing framework.
It has improved my productivity creating webservices in Python dramatically!!
Beta Was this translation helpful? Give feedback.
All reactions