Skip to main content

Library to define and manage daemonized Python-based programs in programmatic (SDK) or CLI-ready ways

Project description

daemonizer

PyPI - Version Linting Docs Ruff Dependabot Updates PyPI - Python Version PyPI - Downloads

Light-weight and easy-to-use Python package that simplifies the process of daemonizing Python applications, allowing them to run in the background as standalone logics.

Features

  • Simple API to daemonize any Python function or class
  • Automatic handling of process management (PID files, logging, etc.)
  • Support for start, stop, restart, and status operations
  • Signal handling (SIGTERM, SIGINT, etc.)
  • Compatible with UNIX-like systems
  • Python 3.7+ support

Getting started

Install package using uv or pip:

uv add daemonizer-py # pip install daemonizer-py

Create a Python script daemon.py where you can define your daemon logic following below framework:

from datetime import datetime
from time import sleep

from daemonizer.core.daemons.logic import forever_loop
from daemonizer.core.daemons.unix import UNIXDaemon

class SampleDaemon(UNIXDaemon):
    """ Sample daemon """

    @forever_loop(catch_exceptions=False, after_delay=0.01)
    def run(self) -> None:
        with open("/tmp/hello_daemon.log", "a") as f:
            f.write(f"Hello, World {datetime.now()}\n")
            f.write(f"{self.kwargs().get('ssa', None)}\n")
        sleep(1)

Defining a daemon is very simple:

  1. Create a subclass of UNIXDaemon
  2. Just overload the run(...) method with your logic to be executed in the daemon

[!NOTE] No need to worry about while True: loop. If you decorate the run(...) method with the @forever_loop(...) decorator, the logic defined inside will be executed infinitely.

Optional arguments are available for the @forever_loop(...) decorator. (See docs)

Once defined, you can start a daemon using two methods:

  • Using handler func:
if __name__ == "__main__":
    handler(SampleDaemon(name="daemon_1"))
uv run daemon.py [start|stop|restart|status]
  • Using context manager handler with DaemonHandler():
from daemonizer.core.handlers.ctx_manager import DaemonHandler
# Daemons handler
with DaemonHandler() as h:
    h.start(SampleDaemon1(name="daemon_1"))
    h.start(SampleDaemon2(name="daemon_2"))

[!WARNING] Daemon should have a unique name at global level (no duplicate).

  • Using CLI:
daemonizer start daemon.py # START op will be performed on all daemons defined in this file
daemonizer start daemon.py SampleDaemon1 # START op will be performed **ONLY** on `SampleDaemon1` defined in this file
  • Using API or web-app (WIP)

The package provides a simple command-line interface to interact with the daemonized process. The following commands are available:

$ python3 main.py {start,stop,restart,status}

Contribute

Clone the repo, improve the code base and submit a pull request

git clone https://github.com/lcsrodriguez/daemonizer.git
cd daemonizer/
uv sync
pre-commit install-hooks

License

This project is licensed under the MIT License - see the LICENSE file for 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

daemonizer_py-1.5.2.tar.gz (59.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

daemonizer_py-1.5.2-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file daemonizer_py-1.5.2.tar.gz.

File metadata

  • Download URL: daemonizer_py-1.5.2.tar.gz
  • Upload date:
  • Size: 59.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for daemonizer_py-1.5.2.tar.gz
Algorithm Hash digest
SHA256 ea49644bf0b413c3538addca339e516f3014e8a94000f07fe2d7db76249ef901
MD5 9630c6651f787156d31d6d35454732c8
BLAKE2b-256 e1578dbdd684bf1f3053ce0693336893c83efdca8b7a546de7fd1e275edbc5fe

See more details on using hashes here.

File details

Details for the file daemonizer_py-1.5.2-py3-none-any.whl.

File metadata

  • Download URL: daemonizer_py-1.5.2-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for daemonizer_py-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9c2699e82a4c9b0e2b3d880824bd0a96d7f9caf0581e989775f39301023b844
MD5 04c065c0cf8963c2f60c01a10a3d0da6
BLAKE2b-256 4e7523b887eecb8f7bc9cb590fdc8596fd16e7b8d73f16db14ec55829b1f33e4

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