Skip to main content

Handle graceful process termination

Project description

Safe Exit is a Python package that provides functionality to handle graceful process termination. The package allows users to register functions that will be called when the program exits.

Different between atexit

Python has standard module atexit do similar thing, but atexit can’t handle when program is killed by a signal not handled by Python.

Python only handle SIGINT signal, don’t handle SIGTERM, SIGQUIT, SIGHUP signals. On Windows, program will also killed by SIGBREAK and CTRL_CLOSE_EVENT.

safe-exit can handle all this signals:

  • On posix system: SIGINT, SIGTERM, SIGQUIT, SIGHUP

  • On windows: SIGINT, SIGTERM, SIGBREAK, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT

Windows also has CTRL_C_EVENT and CTRL_BREAK_EVENT which will translate to SIGINT, SIGBREAK signal by python. On windows, SIGTERM are implemented just for the current process, there is no way to send SIGTERM to other process.

Installation

To install Safe Exit, simply run:

pip install safe-exit

Usage

Just register clean up function like atexit:

import safe_exit

def cleanup_function():
    # Perform cleanup tasks

safe_exit.register(cleanup_function)

register function can also used as function annotation

@safe_exit.register
def cleanup_function():
    # Perform cleanup tasks

Nicely kill a process, giving it a chance to clean up:

process_id = ...
safe_exit.nice_kill(process_id)

Contributing

Contributions to Safe Exit are welcome! Please read the contributing guidelines before submitting a pull request or reporting an issue.

License

Safe Exit is released under the MIT License. See the LICENSE file for more details.

Project details


Download files

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

Source Distribution

safe-exit-0.1.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

safe_exit-0.1.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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