Search code examples
pythonmultiprocessingsignalspython-multiprocessing

How to use more arguments with signal-handler?


Python restricts the signal handler functions ( handling SIGINT, SIGTERM et..) to the following signature with no option to pass extra arguments.

def signal_handler(sig, frame):

In the following scenario when the program consists of multiple processes, I would like to terminate the the processes gracefully in a staggered fashion upon receiving a termination signal.

The problem is when trying to pass the shutdown events and processes to the signal handler, the only way I managed to do so is via using globals.

My question is: In this scenario, how can one avoid using globals?

# shutdown events for graceful termination
taskhandler_shutdown = Event()
logger_shutdown = Event()

# start the processes
p_taskhandler = Process(target=taskhandler.capture, args=[taskhandler_shutdown])
p_taskhandler.start()
p_eventlogger = Process(target=eventlogger.capture, args=[logger_shutdown])
p_eventlogger.start()


def termination_signal_handler(sig, frame):

    # staggered shutdown, first terminate taskhandler
    taskhandler_shutdown.set()
    p_taskhandler.join()

    # now terminate logger process
    logger_shutdown.set()
    p_eventlogger.join()

    sys.exit(0)


Solution

  • The signature for the handler is not really restricted, it's just that it's called with these two arguments passed. You're free to make a signal handler with more parameters and pre-set them with help of partial().

    def sigterm_handler(signum, frame, myobj):
        ...
    
    def register_handler(myobj):
        global sigterm_handler
        sigterm_handler = partial(sigterm_handler, myobj=myobj)
        signal.signal(signal.SIGTERM, sigterm_handler)