Coroutine provider

Coroutine provider creates a coroutine.

importasyncio
fromdependency_injectorimport containers, providers
async defcoroutine(arg1, arg2):
 await asyncio.sleep(0.1)
 return arg1, arg2
classContainer(containers.DeclarativeContainer):
 coroutine_provider = providers.Coroutine(coroutine, arg1=1, arg2=2)
if __name__ == "__main__":
 container = Container()
 arg1, arg2 = asyncio.run(container.coroutine_provider())
 assert (arg1, arg2) == (1, 2)
 assert asyncio.iscoroutinefunction(container.coroutine_provider)

Note

The example works on Python 3.7+. For earlier versions use loop.run_until_complete().

Coroutine provider handles an injection of the dependencies the same way like a Factory provider.

Note

Coroutine provider returns True for asyncio.iscoroutinefunction() check.