Coroutine providers

Coroutine provider create wrapped coroutine on every call.

Coroutine provider is designed for making better integration with asyncio coroutines. In particular, Coroutine provider returns True for asyncio.iscoroutinefunction() checks.


Coroutine provider works only for Python 3.4+.

Example of usage Coroutine provider with async / await-based coroutine:

"""`Coroutine` providers example with async / await syntax.

Current example works only fot Python 3.5+.

import asyncio

import dependency_injector.providers as providers

async def coroutine_function(arg1, arg2):
    """Sample coroutine function."""
    await asyncio.sleep(0.1)
    return arg1, arg2

coroutine_provider = providers.Coroutine(coroutine_function, arg1=1, arg2=2)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    arg1, arg2 = loop.run_until_complete(coroutine_provider())

    assert (arg1, arg2) == (1, 2)
    assert asyncio.iscoroutinefunction(coroutine_provider)

Coroutine providers and injections

Coroutine provider takes a various number of positional and keyword arguments that are used as wrapped coroutine injections. Every time, when Coroutine provider is called, positional and keyword argument injections would be passed as coroutine arguments.

Injections are done according to the next rules:

  • All providers (instances of Provider) are called every time when injection needs to be done.
  • Providers could be injected “as is” (delegated), if it is defined obviously. Check out Coroutine providers delegation.
  • All other injectable values are provided “as is”.
  • Positional context arguments will be appended after Coroutine positional injections.
  • Keyword context arguments have priority on Coroutine keyword injections and will be merged over them.


Examples of making injections could be found in API docs - Coroutine.

Coroutine providers delegation

Coroutine provider could be delegated to any other provider via any kind of injection.

Delegation of Coroutine providers is the same as Factory providers delegation, please follow Factory providers delegation section for examples (with exception of using DelegatedCoroutine instead of DelegatedFactory).

Abstract coroutine providers

AbstractCoroutine provider is a Coroutine provider that must be explicitly overridden before calling.

Behaviour of AbstractCoroutine providers is the same as of AbstractFactory, please follow Abstract factory providers section for examples (with exception of using AbstractCoroutine provider instead of AbstractFactory).