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

Disabling lazy loading in provide #73

Open
IvanKirpichnikov opened this issue Feb 24, 2024 · 2 comments
Open

Disabling lazy loading in provide #73

IvanKirpichnikov opened this issue Feb 24, 2024 · 2 comments
Labels
draft Needs information or coordination with other issues enhancement New feature or request

Comments

@IvanKirpichnikov
Copy link
Contributor

IvanKirpichnikov commented Feb 24, 2024

Idea:
I suggest adding the ability to disable lazy dependency loading for the 'provide' function or globally for the entire 'Provider'

How is it supposed to work?
When creating a new container, create all dependencies with the 'lazy=False' flag and add them to the cache.

Why do you need it?
Create resource-intensive sessions or pools, as well as detect errors early when creating dependencies and protect against errors during code execution

How I see it in the code
Provide

class MyProvider(Provider)
    a = provide(A, scope=Scope.App, lazy=False)

Provider

class MyProvider(Provider)
    lazy = Fasle
    a = provide(A, scope=Scope.App)
@Tishka17
Copy link
Collaborator

Tishka17 commented Feb 24, 2024

Notes:

  1. we probably cannot implement it for async container as it requires async loop, which is often not available at creation point
  2. many real objects are lazy: sqlalchemy engine does not open connections on creation, api clients do not do any requests. So the logic could be more complicated. It can be moved to factory, but anyway
  3. in some cases (lambda functions, pre-fork model in gunicorn and celery with processes) we still want to have lazy initialisation

@Tishka17 Tishka17 added enhancement New feature or request draft Needs information or coordination with other issues labels Feb 24, 2024
@Tishka17
Copy link
Collaborator

Tishka17 commented Mar 4, 2024

We can add separate method, which creates all dependencies of current scope. As it will be called after creation it can be async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft Needs information or coordination with other issues enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants