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
Synchronous setup and teardown for @value decorator. #132
Comments
As it turns out |
Hey @proofit404 , I'm currently window shopping for a DI package for our project and ended up really falling for dependencies, it hits the right balance of brevity and practicality that I need. Handling teardowns is an important part for me (closing database scopes and closing cache processes) and it looks like the feature described in this ticket is exactly what i need. Seeing that this is not yet implemented, is there a way to achieve something similar with the current latest version? with Container as initialized:
initialized.app.process() |
Good morning, Thanks for the interest in the The very first step for this issue to be implemented was done some time ago. You can use Sticky scopes already. It would hold same instances within created scope, so if you call I could suggest to look at handling database transactions example from Also, open and close connections frequently may be not the best idea for performance. You could inject an instance of connection pool in your service objects, so connection management would be hidden in the single place. Last but not least, all complicated part in that milestone relates to the asynchronous implementation. If you need synchronous context only, it maybe worth to implement before you add Hope that helps, feel free to ask more questions if something still not clear for you. Have a good day 🌴 🍸 Best regards, |
# 7.3.0 (2022-06-03) ### Features * setup and teardown of [@value](https://github.com/value) decorator [#132](#132) df62d78
🎉 This issue has been resolved in version 7.3.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@mastern2k3 please take a look at the feature. I would appreciate feedback on how it works and if you find any issues with it, just let me know. Thanks. |
Thanks for the ping @proofit404 , this looks perfect. initialized.app.process()
# connection.release() called after process() ends |
Unfortunately, no. Due to the Python language semantic we could understand only I think we need explicitly deny I'll create a separate ticket with verbose explanation. |
@proofit404, I dove a little deeper and found some more things that I miss. 1. Teardown orderingTake for example this container and dependencies: from dataclasses import dataclass
from dependencies import Injector, value
@dataclass
class One:
def close(self):
pass
@dataclass
class Two:
one: One
def close(self):
pass
@dataclass
class Three:
two: Two
def close(self):
pass
@dataclass
class Thing:
three: Three
class SomeContainer(Injector):
thing = Thing
@value
def one():
print("before one")
yield One()
print("after one")
@value
def two(one: One):
print("before two")
yield Two(one=one)
print("after two")
@value
def three(two: Two):
print("before three")
yield Three(two=two)
print("after three")
with SomeContainer as container:
thing = container.thing
print(f"{thing=}") This code will print the following:
I noticed the dependencies are instantiated in the order of necessity (i.e. I think a reverse order is a more sensible default, or at least, a more commonly desired behavior. A different example could be a set of In this scenario, we will teardown 2. Teardown resiliencyIf an exception occurs in a dependency's teardown code, for example, like so: @value
def two(one: One):
print("before two")
yield Two(one=one)
raise Exception("ha!")
print("after two") The teardown process will cut mid-way:
Having the rest of the dependencies, un-"torndown" (?). This, again in my opinion, is a less desired result. I think that the teardown logic should attempt to teardown all dependencies (by reverse order) while accumulating exceptions if they occur, and if any did, raise them at the end of the process as one aggregated exception. Again the Would love your opinion on this, |
@mastern2k3 thanks for the feedback! It's very informative! I'm appreciated your effort 👍 I'm totally agree with direction of your thoughts on this 👏 Even added one more thing to teardown logic #549 Problems with order and some parts of error handling could be easily addressed. I'm planning to do it during next week. Behavior of collecting errors into some kind of group could take more time, and I still want to give myself a chance to think more about this problem before jumping into implementation of the first thing which came to my mind. I would provide additional details in newly created tickets 🧑🏭 Have a good day 🌴 🍸 Cheers, |
Value decorator should handle generators.
This is very similar to the contextlib.contextmanager.
The text was updated successfully, but these errors were encountered: