From 15c0b00751de548f341a372ad9b767d8aeb19b9d Mon Sep 17 00:00:00 2001 From: Aran Moncusi Ramirez Date: Sat, 22 Feb 2025 19:49:47 +0100 Subject: [PATCH] feat(docs): Added documentation about how to use the init_resources and shutdown_resources using the resource_type argument and how can scope the resources --- docs/providers/resource.rst | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/docs/providers/resource.rst b/docs/providers/resource.rst index 918dfa66..6bea458a 100644 --- a/docs/providers/resource.rst +++ b/docs/providers/resource.rst @@ -210,6 +210,72 @@ first argument. .. _resource-provider-wiring-closing: +Scoping Resources using specialized subclasses +---------------------------------------------- + +You can use specialized subclasses of ``Resource`` provider to initialize and shutdown resources by type. +Allowing for example to only initialize a subgroup of resources. + +.. code-block:: python + + class ScopedResource(resources.Resource): + pass + + def init_service(name) -> Service: + print(f"Init {name}") + yield Service() + print(f"Shutdown {name}") + + class Container(containers.DeclarativeContainer): + + scoped = ScopedResource( + init_service, + "scoped", + ) + + generic = providers.Resource( + init_service, + "generic", + ) + + +To initialize resources by type you can use ``init_resources(resource_type)`` and ``shutdown_resources(resource_type)`` +methods adding the resource type as an argument: + +.. code-block:: python + + def main(): + container = Container() + container.init_resources(ScopedResource) + # Generates: + # >>> Init scoped + + container.shutdown_resources(ScopedResource) + # Generates: + # >>> Shutdown scoped + + +And to initialize all resources you can use ``init_resources()`` and ``shutdown_resources()`` without arguments: + +.. code-block:: python + + def main(): + container = Container() + container.init_resources() + # Generates: + # >>> Init scoped + # >>> Init generic + + container.shutdown_resources() + # Generates: + # >>> Shutdown scoped + # >>> Shutdown generic + + +It works using the :ref:`traverse` method to find all resources of the specified type, selecting all resources +which are instances of the specified type. + + Resources, wiring, and per-function execution scope ---------------------------------------------------