Skip to content
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

Documentation: "Using Scopes to manage Resources" #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bschnitz
Copy link

I tried to make a minimal example on how to automatically free resources with a custom Scope. Since I'm still learning how to use the python-injector, there may be many misconceptions, so please have a firm look.

docs/scopes.rst Outdated
connections. If those resources are shared between multiple classes and should
not be freed and reallocated in between, then using the :class:`SingletonScope` seems
to be the right choice. However resources are not freed automatically afterwards
and bookkeeping must be done manually. However to with some small additions to
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, remove to.

@jstasiak
Copy link
Collaborator

jstasiak commented Jul 7, 2024

Thank you very much for the contribution @bschnitz, it's always nice to see a documentation patch.

May I ask about the exact use case you're addressing here?

Is it primarly/only about being able to do this?

with injector.get(SomeTypeThatNeedsCleanup) as obj:
    obj.doSomething()

If yes, then it seems to me it's enough for SomeTypeThatNeedsCleanup to be a type that's a context manager.

Then injector.get(SomeTypeThatNeedsCleanup) returns a context manager which will be accepted by the with statement and the example should work without any custom scopes or custom Injector wrappers.

@bschnitz
Copy link
Author

bschnitz commented Jul 8, 2024

Hi @jstasiak , thank You for having a look!

Actually, thats not the whole idea and I have to admit, that the example is too short to grasp it correctly. The idea is, that all objects in CleanupScope are handled, not just the one we directly get in this example. So it's really the context of the Scope and not the one of the object, although it looks a bit misleading here. The usage of @contextmanager is a bit misleading, it would be clearer, if it wouldn't have to be used at all, but this crook is needed here.

I will extend the example a bit, then You may have a look again.

Oh, this person named @contextmanager is probably getting lot's of notifications.

Edit: I extended the motivation a bit. It still feels clumsy, but I hope the idea is clearer now. If You have ideas on improving my horrible writing, feel free to tell ;).

@bschnitz bschnitz force-pushed the patch-1 branch 3 times, most recently from 09274c9 to 40533ba Compare July 8, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants