mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-02 03:13:15 +03:00
feat: Add resource_type argument on init_resources
and shutdown_resources
methods to provide a more granular way to initialize the desired resources and add the possibility to scope that ones.
This commit is contained in:
parent
b411807572
commit
23fbb71fde
|
@ -22,7 +22,7 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from typing_extensions import Self as _Self
|
from typing_extensions import Self as _Self
|
||||||
|
|
||||||
from .providers import Provider, ProviderParent, Self
|
from .providers import Provider, Resource, Self, ProviderParent
|
||||||
|
|
||||||
C_Base = TypeVar("C_Base", bound="Container")
|
C_Base = TypeVar("C_Base", bound="Container")
|
||||||
C = TypeVar("C", bound="DeclarativeContainer")
|
C = TypeVar("C", bound="DeclarativeContainer")
|
||||||
|
@ -74,8 +74,8 @@ class Container:
|
||||||
from_package: Optional[str] = None,
|
from_package: Optional[str] = None,
|
||||||
) -> None: ...
|
) -> None: ...
|
||||||
def unwire(self) -> None: ...
|
def unwire(self) -> None: ...
|
||||||
def init_resources(self) -> Optional[Awaitable[None]]: ...
|
def init_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
|
||||||
def shutdown_resources(self) -> Optional[Awaitable[None]]: ...
|
def shutdown_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
|
||||||
def load_config(self) -> None: ...
|
def load_config(self) -> None: ...
|
||||||
def apply_container_providers_overridings(self) -> None: ...
|
def apply_container_providers_overridings(self) -> None: ...
|
||||||
def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
|
def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
|
||||||
|
|
|
@ -315,11 +315,11 @@ class DynamicContainer(Container):
|
||||||
self.wired_to_modules.clear()
|
self.wired_to_modules.clear()
|
||||||
self.wired_to_packages.clear()
|
self.wired_to_packages.clear()
|
||||||
|
|
||||||
def init_resources(self):
|
def init_resources(self, resource_type=providers.Resource):
|
||||||
"""Initialize all container resources."""
|
"""Initialize all container resources."""
|
||||||
futures = []
|
futures = []
|
||||||
|
|
||||||
for provider in self.traverse(types=[providers.Resource]):
|
for provider in self.traverse(types=[resource_type]):
|
||||||
resource = provider.init()
|
resource = provider.init()
|
||||||
|
|
||||||
if __is_future_or_coroutine(resource):
|
if __is_future_or_coroutine(resource):
|
||||||
|
@ -328,7 +328,7 @@ class DynamicContainer(Container):
|
||||||
if futures:
|
if futures:
|
||||||
return asyncio.gather(*futures)
|
return asyncio.gather(*futures)
|
||||||
|
|
||||||
def shutdown_resources(self):
|
def shutdown_resources(self, resource_type=providers.Resource):
|
||||||
"""Shutdown all container resources."""
|
"""Shutdown all container resources."""
|
||||||
def _independent_resources(resources):
|
def _independent_resources(resources):
|
||||||
for resource in resources:
|
for resource in resources:
|
||||||
|
@ -360,7 +360,7 @@ class DynamicContainer(Container):
|
||||||
for resource in resources_to_shutdown:
|
for resource in resources_to_shutdown:
|
||||||
resource.shutdown()
|
resource.shutdown()
|
||||||
|
|
||||||
resources = list(self.traverse(types=[providers.Resource]))
|
resources = list(self.traverse(types=[resource_type]))
|
||||||
if any(resource.is_async_mode_enabled() for resource in resources):
|
if any(resource.is_async_mode_enabled() for resource in resources):
|
||||||
return _async_ordered_shutdown(resources)
|
return _async_ordered_shutdown(resources)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user