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.

Difference from atexit

Python has a standard module called atexit that does something similar, but atexit cannot handle cases where a program is killed by a signal not handled by Python.

Python only handles the SIGINT signal and does not handle SIGTERM, SIGQUIT, and SIGHUP signals. On Windows, programs can also be killed by SIGBREAK and CTRL_CLOSE_EVENT.

Safe Exit can handle all these signals:

  • On POSIX systems: SIGINT, SIGTERM, SIGQUIT, and 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 Python translate to SIGINT and SIGBREAK signals, respectively. On windows, SIGTERM is implemented only for the current process, there is no way to send SIGTERM to other processes.

Installation

To install Safe Exit, simply run:

pip install safe-exit

Usage

Just register a cleanup function like you would with atexit:

import safe_exit

def cleanup_function():
    # Perform cleanup tasks

safe_exit.register(cleanup_function)

The register function can also be used as a decorator:

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

Signal handling is configurable. Call the config function before registering functions. The following code configures safe_exit to handle SIGQUIT and SIGHUP signals:

from safe_exit import ConfigFlag, config, register
config(ConfigFlag.SIGQUIT | ConfigFlag.SIGHUP)

@register
def cleanup()
    print("clean up")

To nicely kill a process, giving it a chance to clean up:

process_id = ...
safe_exit.safe_kill(process_pid)

Contributing

Contributions to Safe Exit are welcome! If you would like to contribute or have any ideas for improvements, please feel free to open an issue on the project’s issue tracker or get in touch with the maintainer directly.

License

Safe Exit is released under the MIT License. See the LICENSE.txt 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.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

safe_exit-0.1.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file safe-exit-0.1.2.tar.gz.

File metadata

  • Download URL: safe-exit-0.1.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for safe-exit-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0cea8e9ffcbb8b98e3b449c272800491327e8823b90472973eafce0fed2feefe
MD5 8fe5df7a0f31042e1adb5eb444aa944d
BLAKE2b-256 5dcad071c346f3f44658ce5da6c91e597e5c19c0c527f519addb07ddbe7cda26

See more details on using hashes here.

File details

Details for the file safe_exit-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: safe_exit-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for safe_exit-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b522c8a540b967b5029e03ed81285707e606459a1b72b8adbbc80e2b18cb9da
MD5 0738a01bd3a2dbbe6baa23cb86fc8f9f
BLAKE2b-256 8e367be794ff9fea258c1c5dab15c2fdad40a3582f852b83eb433a4422e2be09

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