code: async def shutdown(sig, loop):
      
      logging.info(f'Received exit signal {sig.name}...')
      tasks = [task for task in asyncio.Task.all_tasks() if task is not
               asyncio.tasks.Task.current_task()]
  
      for task in tasks:
          logging.debug(f'Cancelling task: {task}')
          task.cancel()
  
      results = await asyncio.gather(*tasks, return_exceptions=True)
      logging.debug(f'Done awaiting cancelled tasks, results: {results}')
  
      loop.stop()
      logging.info('Shutdown complete.')
docs: Gracefully cancel current tasks when app receives a shutdown signal.