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 multiprocess daemons:

https://stackoverflow.com/q/2546276

Usage

from time import sleep
from multiexit import install, register


if __name__ == '__main__':

    # Always call install() on the main process
    install()

    def _subproc1():

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

        sleep(1000)

    subproc1 = Process(
        name='SubProcess1',
        target=_subproc1,
    )
    # 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.
    subproc1.start()

    # Register a cleaner using a decorator
    @register
    def clean_main():
        print('Terminating child {}'.format(
            subproc1.pid,
        ))
        subproc1.terminate()
        subproc1.join()
        print('Child {} ended with {}'.format(
            subproc1.pid,
            subproc1.exitcode,
        ))

    # Wait, and then kill main process
    sleep(3)

    # Suicide
    kill(getpid(), SIGTERM)

For a more extensive example check out example.py.

License

Copyright (C) 2018 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

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
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.0.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file multiexit-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multiexit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11d98fc932a1499ea0e2c9f0b6b397d0e9039c57a8b4317e58e60b77c642d503
MD5 762045440613991c13463f61e7a99b17
BLAKE2b-256 9ac2baa841e0833fc85623fa586697292aa326e0b1bd226c8f025bf3302e7341

See more details on using hashes here.

Supported by

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