Advertisement
ivandev

Untitled

Jul 3rd, 2022
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. async def shutdown(signal, loop):
  2.     logging.info(f"Received exit signal {signal.name}...")
  3.     logging.info("Closing database connections")
  4.     logging.info("Nacking outstanding messages")
  5.     tasks = [t for t in asyncio.all_tasks() if t is not
  6.              asyncio.current_task()]
  7.  
  8.     [task.cancel() for task in tasks]
  9.  
  10.     logging.info(f"Cancelling {len(tasks)} outstanding tasks")
  11.     await asyncio.gather(*tasks, return_exceptions=True)
  12.     logging.info(f"Flushing metrics")
  13.     loop.stop()
  14.  
  15. def add_signal_handler:
  16. for s in signals:
  17.     loop.add_signal_handler(s, lambda s=s: asyncio.create_task(shutdown(s, loop)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement