Skip to content

daemonizer

PyPI - Version Linting Docs Publish 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

It offers a programmatic way or CLI-first interface for developers, without using system-wise solutions like services.

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

  1. Add daemonizer to your project
uv add daemonizer-py

or via pip: pip install daemonizer-py.

  1. Define your daemon logic in a simple Python script sample.py:
Defining daemon logic
from datetime import datetime
from time import sleep

from daemonizer.core.daemons.logic import forever_loop
from daemonizer.core.daemons.unix import UNIXDaemon
from daemonizer.utils.logs import get_logger

logger = get_logger(__name__)

class SampleDaemon(UNIXDaemon):
    @forever_loop(catch_exceptions=False, after_delay=0.01)
    def run(self) -> None:
        with open("/tmp/sample_daemon.log", "a") as f:
            f.write(f"Hello world, this is {datetime.now()}")
        sleep(1)
  1. Interact with it either via the daemonizer CLI
$ daemonizer --no-disclaimer start sample.py SampleDaemon daemon_1

or directly via SDK handler (directly from Python scripts)

Daemon handler
from daemonizer.core.handlers.ctx_manager import DaemonHandler
from .sample import SampleDaemon
from daemonizer.utils.logs import get_logger, setup_logger

setup_logger()
logger = get_logger(__name__)

with DaemonHandler() as h:
    d = SampleDaemon(name="daemon_1")
    h.start(d) # start daemon

Note

Multiple daemons can be defined in a single file and multiple daemons can be managed under a single daemon handler (DaemonHandler instance).