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