From 1b6e3fd2690f5c6cc5bebe0eb8168895a7577702 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 29 Oct 2020 22:53:32 -0400 Subject: [PATCH] Update docs and README --- README.rst | 2 +- docs/index.rst | 3 ++- docs/introduction/key_features.rst | 3 ++- docs/providers/resource.rst | 32 ++++++++++++++++++++++++++++++ docs/wiring.rst | 4 ++++ 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 80f532af..8a221eb8 100644 --- a/README.rst +++ b/README.rst @@ -65,7 +65,7 @@ Key features of the ``Dependency Injector``: - **Containers**. Provides declarative and dynamic containers. See `Containers `_. - **Resources**. Helps with initialization and configuring of logging, event loop, thread - or process pool, etc. + or process pool, etc. Can be used for per-function execution scope in tandem with wiring. See `Resource provider `_. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with other frameworks: Django, Flask, Aiohttp, etc. diff --git a/docs/index.rst b/docs/index.rst index 762bca32..4527a6af 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -73,7 +73,8 @@ Key features of the ``Dependency Injector``: - **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, environment variables and dictionaries. See :ref:`configuration-provider`. - **Resources**. Helps with initialization and configuring of logging, event loop, thread - or process pool, etc. See :ref:`resource-provider`. + or process pool, etc. Can be used for per-function execution scope in tandem with wiring. + See :ref:`resource-provider`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with other frameworks: Django, Flask, Aiohttp, etc. See :ref:`wiring`. diff --git a/docs/introduction/key_features.rst b/docs/introduction/key_features.rst index 26dfa6b9..e586514a 100644 --- a/docs/introduction/key_features.rst +++ b/docs/introduction/key_features.rst @@ -19,7 +19,8 @@ Key features of the ``Dependency Injector``: - **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, environment variables and dictionaries. See :ref:`configuration-provider`. - **Resources**. Helps with initialization and configuring of logging, event loop, thread - or process pool, etc. See :ref:`resource-provider`. + or process pool, etc. Can be used for per-function execution scope in tandem with wiring. + See :ref:`resource-provider`. - **Containers**. Provides declarative and dynamic containers. See :ref:`containers`. - **Wiring**. Injects dependencies into functions and methods. Helps integrating with other frameworks: Django, Flask, Aiohttp, etc. See :ref:`wiring`. diff --git a/docs/providers/resource.rst b/docs/providers/resource.rst index 31bf3d8c..95ec164e 100644 --- a/docs/providers/resource.rst +++ b/docs/providers/resource.rst @@ -203,4 +203,36 @@ first argument. # shutdown ... + +.. _resource-provider-wiring-closing: + +Resources, wiring and per-function execution scope +-------------------------------------------------- + +You can compound ``Resource`` provider with :ref:`wiring` to implement per-function +execution scope. For doing this you need to use additional ``Closing`` marker from +``wiring`` module. + +.. literalinclude:: ../../examples/wiring/flask_resource_closing.py + :language: python + :lines: 3- + :emphasize-lines: 23 + +Framework initializes and injects the resource into the function. With the ``Closing`` marker +framework calls resource ``shutdown()`` method when function execution is over. + +The example above produces next output: + +.. code-block:: bash + + Init service + Shutdown service + 127.0.0.1 - - [29/Oct/2020 22:39:40] "GET / HTTP/1.1" 200 - + Init service + Shutdown service + 127.0.0.1 - - [29/Oct/2020 22:39:41] "GET / HTTP/1.1" 200 - + Init service + Shutdown service + 127.0.0.1 - - [29/Oct/2020 22:39:41] "GET / HTTP/1.1" 200 - + .. disqus:: diff --git a/docs/wiring.rst b/docs/wiring.rst index eeaed305..412ba770 100644 --- a/docs/wiring.rst +++ b/docs/wiring.rst @@ -66,6 +66,10 @@ You can use configuration, provided instance and sub-container providers as you def foo(bar: Bar = Provide[Container.subcontainer.bar]): ... + +You can compound wiring and ``Resource`` provider to implement per-function execution scope. +See :ref:`Resources, wiring and per-function execution scope ` for details. + Wiring with modules and packages --------------------------------