def _cancel_all_tasks(self): to_cancel = tasks.all_tasks(self._loop) if not to_cancel: self._log.info("All tasks finished.") return for task in to_cancel: self._log.warning(f"Cancelling pending task {task}") task.cancel() if self._loop.is_running(): self._log.warning( "Event loop still running during `cancel_all_tasks`.") return finish_all_tasks = tasks.gather(*to_cancel, loop=self._loop, return_exceptions=True) self._loop.run_until_complete(finish_all_tasks) self._log.debug(f"{finish_all_tasks}") for task in to_cancel: if task.cancelled(): continue if task.exception() is not None: self._loop.call_exception_handler({ 'message': 'unhandled exception during asyncio.run() shutdown', 'exception': task.exception(), 'task': task, })
def _cancel_all_tasks(loop: AbstractEventLoop) -> None: """Cancels all tasks the supplied event loop is running :param loop: The event loop to use """ to_cancel = tasks.all_tasks(loop) if not to_cancel: return for task in to_cancel: task.cancel() loop.run_until_complete( tasks.gather(*to_cancel, loop=loop, return_exceptions=True)) for task in to_cancel: if task.cancelled(): continue if task.exception() is not None: loop.call_exception_handler({ "message": "unhandled exception during run_automation() shutdown", "exception": task.exception(), "task": task, })
def _cancel_all_tasks(loop): to_cancel = tasks.all_tasks(loop) if not to_cancel: return for task in to_cancel: task.cancel() loop.run_until_complete( tasks.gather(*to_cancel, loop=loop, return_exceptions=True)) for task in to_cancel: if task.cancelled(): continue if task.exception() is not None: loop.call_exception_handler({ 'message': 'unhandled exception during asyncio.run() shutdown', 'exception': task.exception(), 'task': task, })
def _cancel_all_tasks(self, loop): # https://github.com/python/cpython/blob/7f7dc673540c47db544878bb32d20d9bd1445b94/Lib/asyncio/runners.py#L55 from asyncio import tasks to_cancel = tasks.all_tasks(loop) if not to_cancel: return for task in to_cancel: task.cancel() loop.run_until_complete( tasks.gather(*to_cancel, loop=loop, return_exceptions=True)) for task in to_cancel: if task.cancelled(): continue if task.exception() is not None: loop.call_exception_handler({ "message": "unhandled exception during asyncio.run() shutdown", "exception": task.exception(), "task": task, })
def _all_tasks(loop): if sys.version_info >= (3, 7): return tasks.all_tasks(loop) else: return tasks.Task.all_tasks(loop)