Skip to content

Commit 6f4d23c

Browse files
committed
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.
1 parent 46646b1 commit 6f4d23c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/dependency_injector/containers.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from typing import (
1717
overload,
1818
)
1919

20-
from .providers import Provider, Self, ProviderParent
20+
from .providers import Provider, Resource, Self, ProviderParent
2121

2222

2323
C_Base = TypeVar("C_Base", bound="Container")
@@ -57,8 +57,8 @@ class Container:
5757
def is_auto_wiring_enabled(self) -> bool: ...
5858
def wire(self, modules: Optional[Iterable[Any]] = None, packages: Optional[Iterable[Any]] = None, from_package: Optional[str] = None) -> None: ...
5959
def unwire(self) -> None: ...
60-
def init_resources(self) -> Optional[Awaitable]: ...
61-
def shutdown_resources(self) -> Optional[Awaitable]: ...
60+
def init_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
61+
def shutdown_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
6262
def load_config(self) -> None: ...
6363
def apply_container_providers_overridings(self) -> None: ...
6464
def reset_singletons(self) -> SingletonResetContext[C_Base]: ...

src/dependency_injector/containers.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ class DynamicContainer(Container):
333333
self.wired_to_modules.clear()
334334
self.wired_to_packages.clear()
335335

336-
def init_resources(self):
336+
def init_resources(self, resource_type=providers.Resource):
337337
"""Initialize all container resources."""
338338
futures = []
339339

340-
for provider in self.traverse(types=[providers.Resource]):
340+
for provider in self.traverse(types=[resource_type]):
341341
resource = provider.init()
342342

343343
if __is_future_or_coroutine(resource):
@@ -346,7 +346,7 @@ class DynamicContainer(Container):
346346
if futures:
347347
return asyncio.gather(*futures)
348348

349-
def shutdown_resources(self):
349+
def shutdown_resources(self, resource_type=providers.Resource):
350350
"""Shutdown all container resources."""
351351
def _independent_resources(resources):
352352
for resource in resources:
@@ -378,7 +378,7 @@ class DynamicContainer(Container):
378378
for resource in resources_to_shutdown:
379379
resource.shutdown()
380380

381-
resources = list(self.traverse(types=[providers.Resource]))
381+
resources = list(self.traverse(types=[resource_type]))
382382
if any(resource.is_async_mode_enabled() for resource in resources):
383383
return _async_ordered_shutdown(resources)
384384
else:

0 commit comments

Comments
 (0)