How do you use python-daemon the way the documentation dictates it?

I am trying to make daemon in python and I came across python-daemon package. The interesting thing is that the most common way I used it is not even that the documentation , which is very scarce, tells you to do

import os
import grp
import signal
import daemon
import lockfile

from spam import (
    initial_program_setup,
    do_main_program,
    program_cleanup,
    reload_program_config,
    )

context = daemon.DaemonContext(
    working_directory='/var/lib/foo',
    umask=0o002,
    pidfile=lockfile.FileLock('/var/run/spam.pid'),
    )

context.signal_map = {
    signal.SIGTERM: program_cleanup,
    signal.SIGHUP: 'terminate',
    signal.SIGUSR1: reload_program_config,
    }

mail_gid = grp.getgrnam('mail').gr_gid
context.gid = mail_gid

important_file = open('spam.data', 'w')
interesting_file = open('eggs.data', 'w')
context.files_preserve = [important_file, interesting_file]

initial_program_setup()

with context:
    do_main_program()

Instead, people use it like this:

#!/usr/bin/python
import time
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
    def run(self):
        while True:
            print("Howdy!  Gig'em!  Whoop!")
            time.sleep(10)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

Examples here and in this thread. How do you create a daemon in Python?

So can someone tell me how the package is supposed to be used for its intended purpose? There are 0 examples that use it as indicated in the documentation.

+4
source share
2 answers

-, , , , , , . PEP, , , - , , ... PEP , , , .

A DaemonContext - . API , stdlib. Debian, Ubuntu RedHat/Fedora , , systemd.

A DaemonRunner DaemonContext (ala apachectl). "", .

- "", daemon.DaemonContext, systemd launchd , . , PEP, , , , daemon.

python-daemon . , .

, , . , apachectl -type, , DaemonRunner; , docstrings , , , .

+10

abarnert , python-daemon , , : , .

python-daemon , . - : systemd, init, upstart, launchd .

(), python-daemon . , .

API daemon.daemon, , , .

+3

Source: https://habr.com/ru/post/1589528/


All Articles