Skip to main content

atexit replacement that supports multiprocessing

Project description

A better, saner and more useful atexit replacement for Python 3 that supports multiprocessing.

Inspired by the following StackOverflow question and experience on building multiprocessing daemons:

multiexit will install a handler for the SIGTERM and SIGINT signals and execute the registered exit functions in LIFO order (Last In First Out).

Exit functions can be registered so that only the calling process will call them (the default), or as shared exit functions that will be called by the calling process and all the children subprocesses that inherit it.


multiexit is available for Python 3 from PyPI.

pip3 install multiexit


On the main process, before forking or creating any subprocess, call multiexit.install:

def install(
    signals=(signal.SIGTERM, signal.SIGINT),

Signals to install handler. Usually only SIGTERM and SIGINT are required.


Also install a sys.excepthook that will call the exit functions in case of an unexpected exception.

Then, for each exit function, on any subprocess, call multiexit.register:

register(func, shared=False)

Exit function to register. Any callable without arguments.


If shared, the exit function will be called by the calling process but also by all the children subprocesses that inherit it (thus the ones created after registering it). If shared is False, the default, only the calling process will execute the exit function.


from time import sleep
from signal import SIGTERM
from os import kill, getpid
from multiprocessing import Process

from multiexit import install, register, unregister

if __name__ == '__main__':

    # Always call install() on the main process before creating any
    # subprocess
    # This installs a handler for SIGTERM and SIGINT. Subprocesses will
    # inherit this handler. It also assigns the current PID as the master
    # process, which will allow to choose between exit or os._exit calls
    # when quitting.

    def _subproc1():

        def subproc1_clean():
            print('Subprocess clean!')


    # Register shared exit function so all subprocess call this
    def shared_exit():
        print('Shared exit being called by {} ...'.format(getpid()))

    register(shared_exit, shared=True)

    subproc1 = Process(
    # proc.daemon = True
    # daemon means that signals (like SIGTERM) will be propagated automatically
    # to children. Set to false (the default), to handle the SIGTERM
    # (process.terminate()) to the children yourself.

    # Register a cleaner using a decorator
    def clean_main():
        print('Terminating child {}'.format(
        print('Child {} ended with {}'.format(

    # Wait, and then kill main process

    # Suicide
    kill(getpid(), SIGTERM)

For a more extensive example check out



A SIGINT handler is now installed by default to handle Ctrl+C. This means that Python’s signal.default_int_handler is overridden, thus Ctrl+C no longer raises KeyboardInterrupt. If this behavior is undesired, pass signals=(signal.SIGTERM) when calling install().

Ctrl+C sends SIGINT to all processes in the terminal’s foreground process group, so in order to behave like a SIGTERM shutdown flow, which is sent only on the parent process, the handler will ignore SIGINT on the children to allow the parent to decide what to do with the subprocesses and their corresponding shutdown sequence.

This fixes issue #3


Copyright (C) 2018-2019 KuraLabs S.R.L

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release. See tutorial on generating distribution archives.

Built Distribution

multiexit-1.5.0-py3-none-any.whl (9.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page