aexit called after loop close on death by signal.
This seems odd, the __aexit__ method must be running in a loop because
it is an async function. However if one calls shield then it dies.
'''
$ python3.7 test.py
Hello!
# Ctrl-C before 5 seconds is up
$ python3.7 test.py
^CException ignored in: <coroutine object func at 0x7f0890f08148>
Traceback (most recent call last):
File "test.py", line 18, in func
await asyncio.sleep(5)
File "test.py", line 14, in __aexit__
await asyncio.shield(self.other())
File "/usr/lib/python3.7/asyncio/tasks.py", line 765, in shield
inner = ensure_future(arg, loop=loop)
File "/usr/lib/python3.7/asyncio/tasks.py", line 577, in ensure_future
task = loop.create_task(coro_or_future)
File "/usr/lib/python3.7/asyncio/base_events.py", line 384, in create_task
self._check_closed()
File "/usr/lib/python3.7/asyncio/base_events.py", line 461, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Test.other' was never awaited
'''
import os
import signal
import asyncio
class Test(object):
async def other(self):
print('Hello!')
async def __aenter__(self):
pass
async def __aexit__(self, a, b, c):
await asyncio.shield(self.other())
async def func():
async with Test():
await asyncio.sleep(5)
def main():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(func())
except KeyboardInterrupt:
pass
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
if __name__ == '__main__':
main()
|