utils/__init__.py
def nice_shutdown(shutdown=sys.exit, shutdown_signals=(signal.SIGINT, signal.SIGTERM)):
    """
    Logs shutdown signals nicely, and calls a shutdown function.

    Installs new handlers for the shutdown signals (SIGINT and SIGTERM by default).
    The original handlers are restored before returning.
    """

    shutting_down = False

    def sig_handler(signum, _):
        nonlocal shutting_down

        if shutting_down:
            log.warning('Received signal %(signal)s while shutting down. Aborting.',
                        {'signal': signal.Signals(signum).name})
            sys.exit()

        shutting_down = True

        log.info('Received signal %(signal)s. Shutting down.',
                 {'signal': signal.Signals(signum).name})
        shutdown()

    # Setup new shutdown handlers, storing the old ones for later.
    old_handlers = {}
    for sig in shutdown_signals:
        old_handlers[sig] = signal.signal(sig, sig_handler)

    try:
        yield  # Wrapped code block run here

    finally:
        # Restore the old handlers
        for sig, old_handler in old_handlers.items():
            signal.signal(sig, old_handler)

    Logs shutdown signals nicely, and calls a shutdown function.

    Installs new handlers for the shutdown signals (SIGINT and SIGTERM by default).
    The original handlers are restored before returning.
    
Logs shutdown signals nicely, and calls a shutdown function.
Installs new handlers for the shutdown signals (SIGINT and SIGTERM by default).
The original handlers are restored before returning.
Logs shutdown signals nicely and calls a shutdown function . Installs new handlers for the shutdown signals ( SIGINT and SIGTERM by default ) . The original handlers are restored before returning .
def nice_shutdown(shutdown=sys.exit, shutdown_signals=(signal.SIGINT, signal.SIGTERM)):
    shutting_down = False
    def sig_handler(signum, _):
        nonlocal shutting_down
        if shutting_down:
            log.warning('Received signal %(signal)s while shutting down. Aborting.',
                        {'signal': signal.Signals(signum).name})
            sys.exit()
        shutting_down = True
        log.info('Received signal %(signal)s. Shutting down.',
                 {'signal': signal.Signals(signum).name})
        shutdown()
    old_handlers = {}
    for sig in shutdown_signals:
        old_handlers[sig] = signal.signal(sig, sig_handler)
    try:
        yield  
    finally:
        for sig, old_handler in old_handlers.items():
            signal.signal(sig, old_handler)
def nice_shutdown ( shutdown = sys . exit , shutdown_signals = ( signal . SIGINT , signal . SIGTERM ) ) : shutting_down = False def sig_handler ( signum , _ ) : nonlocal shutting_down if shutting_down : log . warning ( 'Received signal %(signal)s while shutting down. Aborting.' , { 'signal' : signal . Signals ( signum ) . name } ) sys . exit ( ) shutting_down = True log . info ( 'Received signal %(signal)s. Shutting down.' , { 'signal' : signal . Signals ( signum ) . name } ) shutdown ( ) old_handlers = { } for sig in shutdown_signals : old_handlers [ sig ] = signal . signal ( sig , sig_handler ) try : yield finally : for sig , old_handler in old_handlers . items ( ) : signal . signal ( sig , old_handler )
Logs shutdown signals nicely, and calls a shutdown function.
Logs shutdown signals nicely and calls a shutdown function .
"""
    Logs shutdown signals nicely, and calls a shutdown function.

    Installs new handlers for the shutdown signals (SIGINT and SIGTERM by default).
    The original handlers are restored before returning.
    """ # Setup new shutdown handlers, storing the old ones for later. # Wrapped code block run here # Restore the old handlers
shutdown
shutdown_signals