-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Singletons for AsyncIO #105
Comments
Hi! I think this is application/database specific actually and have little to do with inject. Yet, maybe I am wrong. |
Could you give some examples what you are trying to achieve? |
Yep, sure. Imagine we develop a basic web application. In most cases, we need connections to PostgreSQL, Redis, and Kafka (put more of them here). And we may want to inject them at various levels of our application. The good part is that the However, regarding the AsyncIO application, we need to focus on two additional aspects of our connections: how to start and shut them down. The best way to do that is to incorporate such logic into the application lifecycle, start them once on application startup, provide singletons to use in any part of the application, and then gracefully stop them during application shutdown. With the current implementation of the library, I see only two potential solutions:
I think it would be great to have such logic out of the box to cover the application's lifecycle for AsyncIO components. |
Inject is specifically designed as a simple minimalistic DI-framework, not an application framework. You could build your own app with a custom lifecycle using it.
You could use a connection pool and get/release a connection each time. |
Hey!
First of all, thank you for the library; I love the simplicity of the design and how it can be easily used in projects.
However, it seems impossible to have singletons for AsyncIO components. Yes, we can have them as providers and async context managers, as it stated in the examples:
It can be a good approach for simple cases, but I don't want to open database connections each time I call some function. Of course, because of the nature of AsyncIO, I think you stuck to this design, and probably without something like
await inject.wait_shutdown()
with manual control, we can't achieve singletons.Nevertheless, maybe I'm missing something. Is it possible to inject a single database connection without any additional magic?
The text was updated successfully, but these errors were encountered: