|
| 1 | +Coroutine providers |
| 2 | +------------------- |
| 3 | + |
| 4 | +.. currentmodule:: dependency_injector.providers |
| 5 | + |
| 6 | +:py:class:`Coroutine` provider create wrapped coroutine on every call. |
| 7 | + |
| 8 | +:py:class:`Coroutine` provider is designed for making better integration with |
| 9 | +``asyncio`` coroutines. In particular, :py:class:`Coroutine` provider returns |
| 10 | +``True`` for ``asyncio.iscoroutinefunction()`` checks. |
| 11 | + |
| 12 | +.. note:: |
| 13 | + |
| 14 | + :py:class:`Coroutine` provider works only for Python 3.4+. |
| 15 | + |
| 16 | +Example of usage :py:class:`Coroutine` provider with ``async / await``-based |
| 17 | +coroutine: |
| 18 | + |
| 19 | +.. literalinclude:: ../../examples/providers/coroutine_async_await.py |
| 20 | + :language: python |
| 21 | + :linenos: |
| 22 | + |
| 23 | +Coroutine providers and injections |
| 24 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 25 | + |
| 26 | +:py:class:`Coroutine` provider takes a various number of positional and keyword |
| 27 | +arguments that are used as wrapped coroutine injections. Every time, when |
| 28 | +:py:class:`Coroutine` provider is called, positional and keyword argument |
| 29 | +injections would be passed as coroutine arguments. |
| 30 | + |
| 31 | +Injections are done according to the next rules: |
| 32 | + |
| 33 | ++ All providers (instances of :py:class:`Provider`) are called every time |
| 34 | + when injection needs to be done. |
| 35 | ++ Providers could be injected "as is" (delegated), if it is defined obviously. |
| 36 | + Check out :ref:`coroutine_providers_delegation`. |
| 37 | ++ All other injectable values are provided *"as is"*. |
| 38 | ++ Positional context arguments will be appended after :py:class:`Coroutine` |
| 39 | + positional injections. |
| 40 | ++ Keyword context arguments have priority on :py:class:`Coroutine` keyword |
| 41 | + injections and will be merged over them. |
| 42 | + |
| 43 | +.. note:: |
| 44 | + |
| 45 | + Examples of making injections could be found in API docs - |
| 46 | + :py:class:`Coroutine`. |
| 47 | + |
| 48 | +.. _coroutine_providers_delegation: |
| 49 | + |
| 50 | +Coroutine providers delegation |
| 51 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 52 | + |
| 53 | +:py:class:`Coroutine` provider could be delegated to any other provider via |
| 54 | +any kind of injection. |
| 55 | + |
| 56 | +Delegation of :py:class:`Coroutine` providers is the same as |
| 57 | +:py:class:`Factory` providers delegation, please follow |
| 58 | +:ref:`factory_providers_delegation` section for examples (with exception |
| 59 | +of using :py:class:`DelegatedCoroutine` instead of |
| 60 | +:py:class:`DelegatedFactory`). |
| 61 | + |
| 62 | +Abstract coroutine providers |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +:py:class:`AbstractCoroutine` provider is a :py:class:`Coroutine` provider that |
| 66 | +must be explicitly overridden before calling. |
| 67 | + |
| 68 | +Behaviour of :py:class:`AbstractCoroutine` providers is the same as of |
| 69 | +:py:class:`AbstractFactory`, please follow :ref:`abstract_factory_providers` |
| 70 | +section for examples (with exception of using :py:class:`AbstractCoroutine` |
| 71 | +provider instead of :py:class:`AbstractFactory`). |
| 72 | + |
| 73 | +.. disqus:: |
0 commit comments