|
| 1 | +.. _resource-provider: |
| 2 | + |
| 3 | +Resource provider |
| 4 | +================= |
| 5 | + |
| 6 | +.. meta:: |
| 7 | + :keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Resource,Injection, |
| 8 | + Logging,Event Loop,Thread Pool |
| 9 | + :description: Resource provider provides a component with initialization and shutdown. It works |
| 10 | + well for configuring logging, event loop, thread or process pool, etc. |
| 11 | + This page demonstrates how to use resource provider. |
| 12 | + |
| 13 | +.. currentmodule:: dependency_injector.providers |
| 14 | + |
| 15 | +:py:class:`Resource` provider provides a component with initialization and shutdown. |
| 16 | + |
| 17 | +.. literalinclude:: ../../examples/providers/resource.py |
| 18 | + :language: python |
| 19 | + :lines: 3- |
| 20 | + |
| 21 | +Resource providers help to initialize and configure logging, event loop, thread or process pool, etc. |
| 22 | + |
| 23 | +Resource provider is similar to ``Singleton``. Resource initialization happens only once. |
| 24 | +You can do injections and use provided instance the same way like you do with any other provider. |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | + :emphasize-lines: 12 |
| 28 | +
|
| 29 | + class Container(containers.DeclarativeContainer): |
| 30 | +
|
| 31 | + config = providers.Configuration() |
| 32 | +
|
| 33 | + thread_pool = providers.Resource( |
| 34 | + init_threat_pool, |
| 35 | + max_workers=config.max_workers, |
| 36 | + ) |
| 37 | +
|
| 38 | + dispatcher = providers.Factory( |
| 39 | + TaskDispatcher, |
| 40 | + executor=thread_pool, |
| 41 | + ) |
| 42 | +
|
| 43 | +Container has an interface to initialize and shutdown all resources: |
| 44 | + |
| 45 | +.. code-block:: python |
| 46 | +
|
| 47 | + container = Container() |
| 48 | + container.init_resources() |
| 49 | + container.shutdown_resources() |
| 50 | +
|
| 51 | +You also can initialize and shutdown resources one-by-one using ``init()`` and |
| 52 | +``shutdown()`` methods of the provider: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + container = Container() |
| 57 | + container.thread_pool.init() |
| 58 | + container.thread_pool.shutdown() |
| 59 | +
|
| 60 | +Resource provider supports 3 types of initializers: |
| 61 | + |
| 62 | +- Function |
| 63 | +- Generator |
| 64 | +- Subclass of ``resources.Resource`` |
| 65 | + |
| 66 | +Function initializer |
| 67 | +-------------------- |
| 68 | + |
| 69 | +Function is the most common way to specify resource initialization: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + def init_resource(argument1=..., argument2=...): |
| 74 | + return SomeResource() |
| 75 | +
|
| 76 | +
|
| 77 | + class Container(containers.DeclarativeContainer): |
| 78 | +
|
| 79 | + resource = providers.Resource( |
| 80 | + init_resource, |
| 81 | + argument1=..., |
| 82 | + argument2=..., |
| 83 | + ) |
| 84 | +
|
| 85 | +Function initializer may not return a value. This often happens when |
| 86 | +you configure global resource: |
| 87 | + |
| 88 | +.. code-block:: python |
| 89 | +
|
| 90 | + import logging.config |
| 91 | +
|
| 92 | +
|
| 93 | + class Container(containers.DeclarativeContainer): |
| 94 | +
|
| 95 | + configure_logging = providers.Resource( |
| 96 | + logging.config.fileConfig, |
| 97 | + fname='logging.ini', |
| 98 | + ) |
| 99 | +
|
| 100 | +Function initializer does not support shutdown. |
| 101 | + |
| 102 | +Generator initializer |
| 103 | +--------------------- |
| 104 | + |
| 105 | +Resource provider can use 2-step generators: |
| 106 | + |
| 107 | +- First step of generator is an initialization phase |
| 108 | +- The second is step is a shutdown phase |
| 109 | + |
| 110 | +.. code-block:: python |
| 111 | +
|
| 112 | + def init_resource(argument1=..., argument2=...): |
| 113 | + resource = SomeResource() # initialization |
| 114 | +
|
| 115 | + yield resource |
| 116 | +
|
| 117 | + # shutdown |
| 118 | + ... |
| 119 | +
|
| 120 | +
|
| 121 | + class Container(containers.DeclarativeContainer): |
| 122 | +
|
| 123 | + resource = providers.Resource( |
| 124 | + init_resource, |
| 125 | + argument1=..., |
| 126 | + argument2=..., |
| 127 | + ) |
| 128 | +
|
| 129 | +Generator initialization phase ends on the first ``yield`` statement. You can return a |
| 130 | +resource object using ``yield resource`` like in the example above. Returning of the |
| 131 | +object is not mandatory. You can leave ``yield`` statement empty: |
| 132 | + |
| 133 | +.. code-block:: python |
| 134 | +
|
| 135 | + def init_resource(argument1=..., argument2=...): |
| 136 | + # initialization |
| 137 | + ... |
| 138 | +
|
| 139 | + yield |
| 140 | +
|
| 141 | + # shutdown |
| 142 | + ... |
| 143 | +
|
| 144 | +
|
| 145 | + class Container(containers.DeclarativeContainer): |
| 146 | +
|
| 147 | + resource = providers.Resource( |
| 148 | + init_resource, |
| 149 | + argument1=..., |
| 150 | + argument2=..., |
| 151 | + ) |
| 152 | +
|
| 153 | +Subclass initializer |
| 154 | +-------------------- |
| 155 | + |
| 156 | +You can create resource initializer by implementing a subclass of the ``resources.Resource``: |
| 157 | + |
| 158 | +.. code-block:: python |
| 159 | +
|
| 160 | + from dependency_injector import resources |
| 161 | +
|
| 162 | +
|
| 163 | + class MyResource(resources.Resource): |
| 164 | +
|
| 165 | + def init(self, argument1=..., argument2=...) -> SomeResource: |
| 166 | + return SomeResource() |
| 167 | +
|
| 168 | + def shutdown(self, resource: SomeResource) -> None: |
| 169 | + # shutdown |
| 170 | + ... |
| 171 | +
|
| 172 | +
|
| 173 | + class Container(containers.DeclarativeContainer): |
| 174 | +
|
| 175 | + resource = providers.Resource( |
| 176 | + MyResource, |
| 177 | + argument1=..., |
| 178 | + argument2=..., |
| 179 | + ) |
| 180 | +
|
| 181 | +Subclass must implement two methods: ``init()`` and ``shutdown()``. |
| 182 | + |
| 183 | +Method ``init()`` receives arguments specified in resource provider. |
| 184 | +It performs initialization and returns resource object. Returning of the object |
| 185 | +is not mandatory. |
| 186 | + |
| 187 | +Method ``shutdown()`` receives resource object returned from ``init()``. If ``init()`` |
| 188 | +didn't return an object ``shutdown()`` method will be called anyway with ``None`` as a |
| 189 | +first argument. |
| 190 | + |
| 191 | +.. code-block:: python |
| 192 | +
|
| 193 | + from dependency_injector import resources |
| 194 | +
|
| 195 | +
|
| 196 | + class MyResource(resources.Resource): |
| 197 | +
|
| 198 | + def init(self, argument1=..., argument2=...) -> None: |
| 199 | + # initialization |
| 200 | + ... |
| 201 | +
|
| 202 | + def shutdown(self, _: None) -> None: |
| 203 | + # shutdown |
| 204 | + ... |
| 205 | +
|
| 206 | +.. disqus:: |
0 commit comments