code: def _cancel_all_tasks(loop, tasks):
      to_cancel = [task for task in tasks if not task.done()]
  
      if not to_cancel:
          return
  
      for task in to_cancel:
          task.cancel()
  
      loop.run_until_complete(
          asyncio.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,
                  }
              )
  
imports: import asyncio
  import weakref
  from typing import Any, Awaitable, Coroutine, TypeVar, Union
label_desc: Use image node_id 3 for calling a global function with example usage: _cancel_all_tasks(loop, tasks) without return types