如何按照文档说明的方式使用python-daemon? [英] How do you use python-daemon the way that it's documentation dictates?

查看:90
本文介绍了如何按照文档说明的方式使用python-daemon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中创建一个守护进程,并且遇到了python-daemon软件包。有趣的是,我所见过的最常用的方法甚至不是文档非常稀疏,告诉您要做

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

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()

示例此处,在此线程中如何在Python中创建一个守护程序?

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

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.

推荐答案

首先,您不能使用它的原因在我所知的范围内,发现好的文档是,据我所知,没有人写过。当本·芬尼(Ben Finney)提出PEP时,引起了很多兴趣,但是当他要求别人接管该项目并支持该项目时,没有人做,所以……除了PEP和docs direc中的稀疏文档该项目的秘诀,实际上只有什么东西可以解释。

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.

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

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.

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

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.

通常,您不希望这样做-如果需要的话要构建服务,通常只想使用 daemon.DaemonContext 实现该守护程序,然后让 systemd launchd 或其较早的版本通过调用该守护程序来管理该服务。因此,PEP为了简单起见,明确表示服务不在守护程序模块应尝试的范围之内。

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.

但是 python-daemon 发行版中包含服务代码。它尚未完全记录,因为它只是使用守护程序的一种方式的一个示例。

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.

它确实起作用,并且肯定已经过维护和更新这些年。因此,如果您想使用 apachectl 类型的工具,我认为使用 DaemonRunner 是有意义的。只需确保您阅读了文档字符串并编写了一些测试,即可确保它在执行您想要的操作。

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.

这篇关于如何按照文档说明的方式使用python-daemon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆