Created
August 1, 2018 21:21
-
-
Save thehesiod/c2189c41dabdde133a980c62cb33da40 to your computer and use it in GitHub Desktop.
asyncio generator issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
async def generator_fn(): | |
print(f" enter generator {id(asyncio.Task.current_task())}") | |
try: | |
while True: | |
yield | |
except: | |
print(f" except generator {id(asyncio.Task.current_task())}") | |
raise | |
finally: | |
print(f" finally generator {id(asyncio.Task.current_task())}") | |
async def caller(): | |
print(f" enter caller {id(asyncio.Task.current_task())}") | |
try: | |
async for _ in generator_fn(): | |
break | |
finally: | |
print(f" finally caller {id(asyncio.Task.current_task())}") | |
async def main(): | |
print(f"enter main {id(asyncio.Task.current_task())}") | |
try: | |
await asyncio.ensure_future(caller()) | |
finally: | |
print(f"exit main {id(asyncio.Task.current_task())}") | |
asyncio.get_event_loop().run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This generates output like the following:
notice how the generator except/finally don't have the same task as when it was entered, and further that it's a completely different task that I did not create.