2020-09-02 01:39:23 +00:00
|
|
|
"""`Coroutine` providers example with async / await syntax."""
|
2018-10-18 16:39:19 +00:00
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2020-09-03 20:59:11 +00:00
|
|
|
from dependency_injector import containers, providers
|
2018-10-18 16:39:19 +00:00
|
|
|
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
async def coroutine(arg1, arg2):
|
|
|
|
await asyncio.sleep(0.1)
|
2018-10-18 16:39:19 +00:00
|
|
|
return arg1, arg2
|
|
|
|
|
|
|
|
|
2020-09-03 20:59:11 +00:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
coroutine_provider = providers.Coroutine(coroutine, arg1=1, arg2=2)
|
2018-10-18 16:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-09-03 20:59:11 +00:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
arg1, arg2 = asyncio.run(container.coroutine_provider())
|
2018-10-18 16:39:19 +00:00
|
|
|
assert (arg1, arg2) == (1, 2)
|
2020-09-03 20:59:11 +00:00
|
|
|
assert asyncio.iscoroutinefunction(container.coroutine_provider)
|