Proper way to shutdown asyncio tasks

The cancellation is not immediate and requires running ioloop to be resolved with exception CancelledError. Remove ioloop.stop from shutdown and handle exception in supervisor, to make things work. Below simplified example. Important is, however you can cancel Task, it only stops watching/waiting for end/results and loop won’t handle further events for it. But the underneath … Read more

Asyncio: Weirdness of Task exception was never retrieved

A Exception in the Task (of underlying asyncio.Future to be precise) can be retrieved with Future.exception(). If it’s not retrieved, the exception will be handled at release of the Future object with eventloop’s call_exception_handler. So, as @dirn pointed, while the Task has reference (assigned to variable in your case) it’s not going be freed, therefore … Read more

Start async function without importing the asyncio package

Of course it is possible to start an async function without explicitly using asyncio. After all, asyncio is written in Python, so all it does, you can do too (though sometimes you might need other modules like selectors or threading if you intend to concurrently wait for external events, or paralelly execute some other code). … Read more

How to await a coroutine in pdb

I had a similar problem debugging the useage of aiofile. I then found a solution using nest_asyncio. For example if one has the following async example script: import asyncio from aiofile import async_open import nest_asyncio async def main(): async with async_open(“/tmp/hello.txt”, ‘w+’) as afp: await afp.write(“Hello “) await afp.write(“world”) afp.seek(0) breakpoint() print(await afp.read()) if __name__==”__main__”: … Read more

asyncio create_task to run forever

I expected the numbers to keep printing even after some_func is completed. The argument to run_until_complete controls how long the event loop is run. And once the event loop stops running, all coroutines are effectively suspended, not just the one that you’ve been waiting for. But you do have different options: loop.run_until_complete(some_func()) – what you … Read more

What are all these deprecated “loop” parameters in asyncio?

What problems did loop solve? Why would one have used it in the first place? Prior to Python 3.6, asyncio.get_event_loop() was not guaranteed to return the event loop currently running when called from an asyncio coroutine or callback. It would return whatever event loop was previously set using set_event_loop(some_loop), or the one automatically created by … Read more

How to gracefully terminate an asyncio script with Ctrl-C?

Use signal handlers: import asyncio from signal import SIGINT, SIGTERM async def main_coro(): try: await awaitable() except asyncio.CancelledError: do_cleanup() if __name__ == “__main__”: loop = asyncio.get_event_loop() main_task = asyncio.ensure_future(main_coro()) for signal in [SIGINT, SIGTERM]: loop.add_signal_handler(signal, main_task.cancel) try: loop.run_until_complete(main_task) finally: loop.close()