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

Release injected object resource on scope exit #119

Closed
koi8-r opened this issue Jun 15, 2019 · 2 comments
Closed

Release injected object resource on scope exit #119

koi8-r opened this issue Jun 15, 2019 · 2 comments

Comments

@koi8-r
Copy link

koi8-r commented Jun 15, 2019

I want to inject a database connection during processing of http request in the custom request scope. Where i can close the connection after request is processed and request scope exits. Can i do this? And can i use custom scope for this?

@jstasiak
Copy link
Collaborator

You can close the connection explicitly after you're done using it. Or instead of opening/allocating a connection right away you can inject a context manager that lets you manage the connection. Or you can provide an after-request hook that requests the connection object and closes it there. Possibly other ways too, totally up to your code.

Yes, you could (ab)use a custom scope for this – see https://github.com/alecthomas/flask_injector/blob/18282a42539c8a8809feff1f466f763116e10bda/flask_injector.py (look for clenaup) and, quite related, python-injector/flask_injector#37.

@koi8-r
Copy link
Author

koi8-r commented Jun 17, 2019

Exactly what is needed. Thanks a lot for the examples.

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

No branches or pull requests

2 participants