For some reason this program prints the following warning:
Task exception was never retrieved
future: <Task finished coro=<coro() done, defined at /usr/lib64/python3.4/asyncio/coroutines.py:139> exception=SystemExit(2,)>
even though the exception is clearly retrieved and propagated, as caught SystemExit! is printed to the terminal, and process status code becomes 2.
The same thing happens with Python 2 and trollius.
Am I missing something?
#!/usr/bin/env python3
import asyncio
@asyncio.coroutine
def comain():
raise SystemExit(2)
def main():
loop = asyncio.get_event_loop()
task = loop.create_task(comain())
try:
loop.run_until_complete(task)
except SystemExit:
print("caught SystemExit!")
raise
finally:
loop.close()
if __name__ == "__main__":
main()
loop.run_until_complete(comain())which works as expected?task.cancel()in aKeyboardInterrupthandler (not shown in this snippet).if not task.cancelled(): task.result()into the finally block. Though I don't know whether it is the intended behavior that you have to calltask.result()ortask.exception()manually (I would expectrun_until_complete()do it for you).