Future/generator expansion inside asyncio.coroutine
import asyncio | |
import contextlib | |
@asyncio.coroutine | |
def return_coroutine_object(): | |
@asyncio.coroutine | |
def g(): | |
yield from asyncio.sleep(0.01) | |
return "return_coroutine_object" | |
return g() | |
@asyncio.coroutine | |
def return_future(): | |
fut = asyncio.Future() | |
fut.set_result("return_future") | |
return fut | |
@asyncio.coroutine | |
def generator_return_future(): | |
yield from asyncio.sleep(0.01) | |
fut = asyncio.Future() | |
fut.set_result("generator_return_future") | |
return fut | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
with contextlib.closing(loop): | |
print("return_future:", | |
loop.run_until_complete(return_future())) | |
print("generator_return_future:", | |
loop.run_until_complete(generator_return_future())) | |
print("return_coroutine_object:", | |
loop.run_until_complete(return_coroutine_object())) |
$ PYTHONASYNCIODEBUG=X python3 return_await.py | |
<CoroWrapper return_coroutine_object.<locals>.g() running at return_await.py:7, created at return_await.py:12> was never yielded from | |
Coroutine object created at (most recent call last): | |
File "return_await.py", line 44, in <module> | |
loop.run_until_complete(return_coroutine_object())) | |
File "/usr/lib/python3.4/asyncio/base_events.py", line 304, in run_until_complete | |
self.run_forever() | |
File "/usr/lib/python3.4/asyncio/base_events.py", line 276, in run_forever | |
self._run_once() | |
File "/usr/lib/python3.4/asyncio/base_events.py", line 1164, in _run_once | |
handle._run() | |
File "/usr/lib/python3.4/asyncio/events.py", line 120, in _run | |
self._callback(*self._args) | |
File "/usr/lib/python3.4/asyncio/tasks.py", line 238, in _step | |
result = next(coro) | |
File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__ | |
return next(self.gen) | |
File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro | |
res = func(*args, **kw) | |
File "return_await.py", line 12, in return_coroutine_object | |
return g() | |
return_future: return_future | |
generator_return_future: <Future finished result='generator_return_future' created at return_await.py:27> | |
return_coroutine_object: <CoroWrapper return_coroutine_object.<locals>.g() running at return_await.py:7, created at return_await.py:12> | |
$ |
$ python3 return_await.py | |
return_future: return_future | |
generator_return_future: <Future finished result='generator_return_future'> | |
return_coroutine_object: return_coroutine_object | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment