你如何按照文档规定的方式使用python-daemon?

时间:2021-05-02 23:18:03

I'm trying to make a daemon in python and I've come across the python-daemon package. The interesting thing about it is that the most common way I've seen it used isn't even what the documentation, which is very sparse, tells you to do

我正在尝试在python中创建一个守护进程,我遇到了python-daemon包。有趣的是,我看到它使用的最常见的方式甚至不是那些非常稀疏的文档告诉你做的事情

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?

此处和此线程中的示例如何在Python中创建守护程序?

So can anyone tell me how the package is supposed to be used as intended? There are 0 examples to be found that use it the way the documentation specifies.

那么有谁能告诉我如何按预期使用包装?有0个示例可以按文档指定的方式使用它。

2 个解决方案

#1


11  

First, the reason you can't find good documentation is that, to the best of my knowledge, nobody ever wrote it. When Ben Finney proposed the PEP, there was plenty of interest, but then when he asked for someone else to take over the project and champion it, nobody did, so… beyond the PEP, and the sparse documentation in the docs directory of the project, there's really nothing but the source to explain things.

首先,你无法找到好的文档的原因是,据我所知,没有人写过它。当Ben Finney提出PEP时,有很多兴趣,但是当他要求其他人接管项目并支持它时,没有人这样做,所以...超出PEP,以及项目docs目录中的稀疏文档,除了解释事物的来源之外别无他物。

A DaemonContext is the way you're meant to create a daemon. Its API was bikeshedded extensively, and was the only part that was proposed to be part of the public interface in the stdlib. People from the Debian, Ubuntu, and RedHat/Fedora projects were involved in the initial discussion, and changes have been incorporated based on their experiences moving their distros to systemd.

DaemonContext是您创建守护进程的方式。它的API被广泛使用,并且是唯一被提议成为stdlib中公共接口一部分的部分。来自Debian,Ubuntu和RedHat / Fedora项目的人员参与了初步讨论,并根据他们将发行版移动到systemd的经验进行了更改。

A DaemonRunner wraps up a DaemonContext-based daemon and a control tool (ala apachectl). This implements a “service”, which is only one way of running a daemon out of many other different ways.

DaemonRunner包装了一个基于DaemonContext的守护进程和一个控制工具(ala apachectl)。这实现了一个“服务”,这只是从许多其他不同方式运行守护进程的一种方式。

Often, you don't want this—if you want to build a "service", you usually want to only implement the daemon using a daemon.DaemonContext, and let systemd or launchd or their older predecessors manage the service by invoking that daemon. So, the PEP, to keep things simple, explicitly said that a service is outside the scope of what the daemon module should attempt.

通常,您不希望这样 - 如果您想构建“服务”,您通常只想使用daemon.DaemonContext实现守护程序,并让systemd或launchd或其旧的前任通过调用该守护进程来管理服务。因此,为简单起见,PEP明确表示服务超出了守护程序模块应该尝试的范围。

But there is code for services in the python-daemon distribution. It isn't fully documented, because it is only an example of one way to use a daemon.

但是python-daemon发行版中有服务代码。它没有完整记录,因为它只是使用守护进程的一种方法的示例。

It does appear to work, and it's definitely been maintained and updated over the years. So, if you want an apachectl-type tool, I think it makes sense to use a DaemonRunner; just make sure you read the docstrings and write some tests to make sure it's doing what you wanted.

它似乎确实有效,并且多年来它一直在维护和更新。所以,如果你想要一个apachectl类型的工具,我认为使用DaemonRunner是有意义的;只要确保你阅读文档字符串并编写一些测试,以确保它正在做你想要的。

#2


3  

As abarnert says, the python-daemon documentation is meant to show how to write a daemon process: a program that detaches itself from the controlling terminal and has no parent process and runs in the background.

正如abarnert所说,python-daemon文档旨在展示如何编写一个守护进程:一个程序,它将自己与控制终端分离,没有父进程并在后台运行。

The python-daemon code doesn't dictate how to use that daemon in a service. To implement the service, you need something to run that daemon: systemd, init, upstart, launchd all can do the job.

python-daemon代码没有规定如何在服务中使用该守护进程。要实现该服务,您需要运行该守护进程:systemd,init,upstart,launchd all都可以完成这项工作。

As an (undocumented) example, the python-daemon code base includes a simple example runner. It is not part of writing a daemon, and better tools exist.

作为(未记录的)示例,python-daemon代码库包含一个简单的示例运行器。它不是编写守护进程的一部分,而是存在更好的工具。

Write the daemon using the daemon.daemon API as documented, and run it using a runner that comes with your operating system.

使用记录的daemon.daemon API编写守护程序,并使用操作系统附带的运行程序运行它。

#1


11  

First, the reason you can't find good documentation is that, to the best of my knowledge, nobody ever wrote it. When Ben Finney proposed the PEP, there was plenty of interest, but then when he asked for someone else to take over the project and champion it, nobody did, so… beyond the PEP, and the sparse documentation in the docs directory of the project, there's really nothing but the source to explain things.

首先,你无法找到好的文档的原因是,据我所知,没有人写过它。当Ben Finney提出PEP时,有很多兴趣,但是当他要求其他人接管项目并支持它时,没有人这样做,所以...超出PEP,以及项目docs目录中的稀疏文档,除了解释事物的来源之外别无他物。

A DaemonContext is the way you're meant to create a daemon. Its API was bikeshedded extensively, and was the only part that was proposed to be part of the public interface in the stdlib. People from the Debian, Ubuntu, and RedHat/Fedora projects were involved in the initial discussion, and changes have been incorporated based on their experiences moving their distros to systemd.

DaemonContext是您创建守护进程的方式。它的API被广泛使用,并且是唯一被提议成为stdlib中公共接口一部分的部分。来自Debian,Ubuntu和RedHat / Fedora项目的人员参与了初步讨论,并根据他们将发行版移动到systemd的经验进行了更改。

A DaemonRunner wraps up a DaemonContext-based daemon and a control tool (ala apachectl). This implements a “service”, which is only one way of running a daemon out of many other different ways.

DaemonRunner包装了一个基于DaemonContext的守护进程和一个控制工具(ala apachectl)。这实现了一个“服务”,这只是从许多其他不同方式运行守护进程的一种方式。

Often, you don't want this—if you want to build a "service", you usually want to only implement the daemon using a daemon.DaemonContext, and let systemd or launchd or their older predecessors manage the service by invoking that daemon. So, the PEP, to keep things simple, explicitly said that a service is outside the scope of what the daemon module should attempt.

通常,您不希望这样 - 如果您想构建“服务”,您通常只想使用daemon.DaemonContext实现守护程序,并让systemd或launchd或其旧的前任通过调用该守护进程来管理服务。因此,为简单起见,PEP明确表示服务超出了守护程序模块应该尝试的范围。

But there is code for services in the python-daemon distribution. It isn't fully documented, because it is only an example of one way to use a daemon.

但是python-daemon发行版中有服务代码。它没有完整记录,因为它只是使用守护进程的一种方法的示例。

It does appear to work, and it's definitely been maintained and updated over the years. So, if you want an apachectl-type tool, I think it makes sense to use a DaemonRunner; just make sure you read the docstrings and write some tests to make sure it's doing what you wanted.

它似乎确实有效,并且多年来它一直在维护和更新。所以,如果你想要一个apachectl类型的工具,我认为使用DaemonRunner是有意义的;只要确保你阅读文档字符串并编写一些测试,以确保它正在做你想要的。

#2


3  

As abarnert says, the python-daemon documentation is meant to show how to write a daemon process: a program that detaches itself from the controlling terminal and has no parent process and runs in the background.

正如abarnert所说,python-daemon文档旨在展示如何编写一个守护进程:一个程序,它将自己与控制终端分离,没有父进程并在后台运行。

The python-daemon code doesn't dictate how to use that daemon in a service. To implement the service, you need something to run that daemon: systemd, init, upstart, launchd all can do the job.

python-daemon代码没有规定如何在服务中使用该守护进程。要实现该服务,您需要运行该守护进程:systemd,init,upstart,launchd all都可以完成这项工作。

As an (undocumented) example, the python-daemon code base includes a simple example runner. It is not part of writing a daemon, and better tools exist.

作为(未记录的)示例,python-daemon代码库包含一个简单的示例运行器。它不是编写守护进程的一部分,而是存在更好的工具。

Write the daemon using the daemon.daemon API as documented, and run it using a runner that comes with your operating system.

使用记录的daemon.daemon API编写守护程序,并使用操作系统附带的运行程序运行它。