mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-12 09:02:24 +03:00
Add coroutine provider examples
This commit is contained in:
parent
ac0e5eb26a
commit
9874624d94
26
examples/providers/coroutine.py
Normal file
26
examples/providers/coroutine.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
"""`Coroutine` providers example with @asyncio.coroutine decorator.
|
||||||
|
|
||||||
|
Current example works only fot Python 3.4+.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def coroutine_function(arg1, arg2):
|
||||||
|
"""Example coroutine function."""
|
||||||
|
yield from 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)
|
25
examples/providers/coroutine_async_await.py
Normal file
25
examples/providers/coroutine_async_await.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
"""`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):
|
||||||
|
"""Example 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)
|
Loading…
Reference in New Issue
Block a user