When using asyncio, how do you allow all running tasks to finish before shutting down the event loop
问题 I have the following code: @asyncio.coroutine def do_something_periodically(): while True: asyncio.async(my_expensive_operation()) yield from asyncio.sleep(my_interval) if shutdown_flag_is_set: print("Shutting down") break I run this function until complete. The problem occurs when shutdown is set - the function completes and any pending tasks are never run. (You see this as an error task: <Task pending coro=<report() running at script.py:33> wait_for=<Future pending cb=[Task._wakeup()]>> ).