在谷歌上搜索显示x2个代码片段。第一个结果是这个代码配方,它有很多文档和解释,以及下面一些有用的讨论。

然而,另一个代码示例虽然不包含这么多文档,但包含了传递启动、停止和重启等命令的示例代码。它还创建了一个PID文件,可以方便地检查守护进程是否已经在运行等。

这些示例都解释了如何创建守护进程。还有其他需要考虑的事情吗?一个样本比另一个更好吗?为什么?


当前回答

另一种方法——创建一个正常的、非守护的Python程序,然后使用监控器在外部对其进行守护。这可以省去很多麻烦,并且是*nix和语言可移植的。

其他回答

这个函数将应用程序转换为守护进程:

import sys
import os

def daemonize():
    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
    except OSError as err:
        sys.stderr.write('_Fork #1 failed: {0}\n'.format(err))
        sys.exit(1)
    # decouple from parent environment
    os.chdir('/')
    os.setsid()
    os.umask(0)
    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            # exit from second parent
            sys.exit(0)
    except OSError as err:
        sys.stderr.write('_Fork #2 failed: {0}\n'.format(err))
        sys.exit(1)
    # redirect standard file descriptors
    sys.stdout.flush()
    sys.stderr.flush()
    si = open(os.devnull, 'r')
    so = open(os.devnull, 'w')
    se = open(os.devnull, 'w')
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

这是我的基本'Howdy World' Python守护进程,当我开发一个新的守护进程应用程序时,我开始使用它。

#!/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-daemon库。你可以通过以下方法安装:

pip install python-daemon

然后用。/howdy.py start开始,用。/howdy.py stop结束。

可能这不是对问题的直接回答,但systemd可以用于作为守护进程运行应用程序。这里有一个例子:

[Unit]
Description=Python daemon
After=syslog.target
After=network.target

[Service]
Type=simple
User=<run as user>
Group=<run as group group>
ExecStart=/usr/bin/python <python script home>/script.py

# Give the script some time to startup
TimeoutSec=300

[Install]
WantedBy=multi-user.target

我更喜欢这种方法,因为许多工作都是为您完成的,并且您的守护进程脚本的行为与系统的其他部分类似。

恐怕@Dustin提到的守护模块对我不起作用。相反,我安装了python-daemon并使用以下代码:

# filename myDaemon.py
import sys
import daemon
sys.path.append('/home/ubuntu/samplemodule') # till __init__.py
from samplemodule import moduleclass 

with daemon.DaemonContext():
    moduleclass.do_running() # I have do_running() function and whatever I was doing in __main__() in module.py I copied in it.

跑步很容易

> python myDaemon.py

为了完整起见,这里是samplemodule的目录内容

>ls samplemodule
__init__.py __init__.pyc moduleclass.py

py的内容可以是

class moduleclass():
    ...

def do_running():
    m = moduleclass()
    # do whatever daemon is required to do.

用Python创建守护进程最简单的方法是使用Twisted事件驱动框架。它为您处理守护进程所需的所有东西。它使用反应堆模式来处理并发请求。