Skip to main content

A signal handler utility for Python applications running as systemd units

Project description

systemd-stopper is a small library for conveniently handling the systemctl stop operation in Python application running as systemd units. Since running a Python process with systemd is simple, handling the stop command should be simple as well.

Quickstart

Consider the following unit-file defining my_app.service:

[Unit]
Description=Cool Python Unit
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/user/my_app.py
KillMode=Process

[Install]
WantedBy=multi-user.target

To stop the application gracefully via systemctl stop my_app, we have to implement a signal handler for the SIGTERM signal. Using the signal module from the standard library directly is a bit low-level, escpecially if your application is multithreaded or if you have to implement the signal handling in multiple applications.

systemd-stopper lets you handle the stop event straightforward, for example in an application with a simple main loop:

import systemd_stopper
stopper = systemd_stopper.install()
while stopper.run:
    do_stuff()
handle_graceful_shutdown()

The install() function sets up the signal handlers and returns a Stopper object to query the status of the stop command. You have multiple options:

# Use other signals for stopping the application.
stopper = systemd_stopper.install('USR1', 'HUP')

# Wait for a threading.Event instead of polling the run attribute.
stopper.event.wait() # returns when the stop signal has been receive

# Use a callback function that gets called exactly once, which is useful
# for single-threaded IO loops like those of tornado or asyncio
ioloop = framework_of_choice.get_io_loop()
systemd_stopper.install(callback=ioloop.stop)

Installation

pip install systemd-stopper

Further documentation

Further documentation will be added when I have the time to write it.

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

systemd-stopper-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file systemd-stopper-0.1.0.tar.gz.

File metadata

File hashes

Hashes for systemd-stopper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8f10983de5caa0a09b7155c9c40a99c959db27cac1b0bc12d2725861cbb5bb31
MD5 5b2b1f3324dcf8145478246708d52983
BLAKE2b-256 d96ebd57fbd3e09fbab4f3f4c5b7c31bb5e8b737fbe8892eb95dc72132bf57df

See more details on using hashes here.

Supported by

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