Replies: 3 comments 7 replies
-
Yeah that looks good 😊 As for the name, could be a longlived_service, or maybe an application_service (although that might be confusing)? |
Beta Was this translation helpful? Give feedback.
0 replies
-
better name idea: |
Beta Was this translation helpful? Give feedback.
0 replies
-
So, as a datapoint, here is what I ended up adding in my fastapi lifespan function, as a way to avoid initializing an engine as a side-effect of initializing the registry. @functools.cache
def get_engine() -> AsyncEngine:
return create_async_engine("...")
registry.register_factory(
AsyncEngine,
get_engine,
on_registry_close=lambda: get_engine().dispose,
) This allows me to easily replace the engine in the tests with lifespan.registry.register_value(AsyncEngine, pytest_managed_postgres_engine) |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This idea is based on #8 (comment) and @elfjes concerns about container lifetimes.
Currently, svcs allows to register
on_registry_close
cleanups, but that's only necessary if an object created outside of svcs. For me that's common, but maybe just because I'm used to it.The idea is to add a third kind of service, the singleton. A singleton is a factory that is only called once it is needed and then it lives as long as the registry.
So instead of:
You'd write:
This allows to late-bind base/Service and integrates the engine more seamlessly into the whole life-cycle
Is this fit your vision @elfjes?
I'm also open to a better name, because surely there's some hairsplitting to be done about the term "singleton".
Beta Was this translation helpful? Give feedback.
All reactions