Python守护程序使用相同的对象列表

时间:2018-01-16 19:09:55

标签: python python-3.x daemon python-daemon

我有一个名为People的班级。这个班有一个清单。我不希望将这个列表保存在文件或数据库中,都在内存中,所以我认为可行的方法是创建一个守护进程并保持进程打开,这是我的代码:

daemon.py

# coding: utf-8
import os
import sys
import time
import atexit
import signal
from people import People

class Daemon(object):
    """
    A generic daemon class.
    Usage: subclass the Daemon class and override the run() method
    """

    def __init__(self, pidfile, stdin='/dev/null',
                 stdout='/dev/null', stderr='/dev/null'):
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.pidfile = pidfile
        self.bc = People()

    def daemonize(self):
        """
        do the UNIX double-fork magic, see Stevens' "Advanced
        Programming in the UNIX Environment" for details (ISBN 0201563177)
        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
        """
        # Do first fork
        self.fork()

        # Decouple from parent environment
        self.dettach_env()

        # Do second fork
        self.fork()

        # Flush standart file descriptors
        sys.stdout.flush()
        sys.stderr.flush()

        #
        self.attach_stream('stdin', mode='r')
        self.attach_stream('stdout', mode='a+')
        self.attach_stream('stderr', mode='a+')

        # write pidfile
        self.create_pidfile()

    def attach_stream(self, name, mode):
        """
        Replaces the stream with new one
        """
        stream = open(getattr(self, name), mode)
        os.dup2(stream.fileno(), getattr(sys, name).fileno())

    def dettach_env(self):
        os.chdir("/")
        os.setsid()
        os.umask(0)

    def fork(self):
        """
        Spawn the child process
        """
        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError as e:
            sys.stderr.write("Fork failed: %d (%s)\n" % (e.errno, e.strerror))
            sys.exit(1)

    def create_pidfile(self):
        atexit.register(self.delpid)
        pid = str(os.getpid())
        open(self.pidfile, 'w+').write("%s\n" % pid)

    def delpid(self):
        """
        Removes the pidfile on process exit
        """
        os.remove(self.pidfile)

    def start(self):
        """
        Start the daemon
        """
        # Check for a pidfile to see if the daemon already runs
        pid = self.get_pid()

        if pid:
            message = "pidfile %s already exist. Daemon already running?\n"
            sys.stderr.write(message % self.pidfile)
            sys.exit(1)

        # Start the daemon
        self.daemonize()
        self.run()

    def get_pid(self):
        """
        Returns the PID from pidfile
        """
        try:
            pf = open(self.pidfile, 'r')
            pid = int(pf.read().strip())
            pf.close()
        except (IOError, TypeError):
            pid = None
        return pid

    def stop(self, silent=False):
        """
        Stop the daemon
        """
        # Get the pid from the pidfile
        pid = self.get_pid()

        if not pid:
            if not silent:
                message = "pidfile %s does not exist. Daemon not running?\n"
                sys.stderr.write(message % self.pidfile)
            return  # not an error in a restart

        # Try killing the daemon process
        try:
            while True:
                os.kill(pid, signal.SIGTERM)
                time.sleep(0.1)
        except OSError as err:
            err = str(err)
            if err.find("No such process") > 0:
                if os.path.exists(self.pidfile):
                    os.remove(self.pidfile)
            else:
                sys.stdout.write(str(err))
                sys.exit(1)

    def restart(self):
        """
        Restart the daemon
        """
        self.stop(silent=True)
        self.start()

    def run(self):
        """
        You should override this method when you subclass Daemon. It will be called after the process has been
        daemonized by start() or restart().
        """
        raise NotImplementedError

我的主要档案:

# coding: utf-8
import argparse
import sys
import time
from people import People
import logging
from daemon import Daemon


class MyDaemon(Daemon):
    def run(self):
        while True:
            logging.debug("I'm here...")
            time.sleep(1)

    def get_people(self):
        return self.bc



def main():
    """
    The application entry point
    """
    parser = argparse.ArgumentParser(
        description='Daemon runner',
        epilog="That's all folks"
    )

    parser.add_argument(
        'operation',
        metavar='OPERATION',
        type=str,
        help='Operation with daemon. Accepts any of these values: start, stop, restart, status',
        choices=['start', 'stop', 'restart', 'status', 'printpeople', 'add1', 'add2', 'add3', 'add4']
    )

    args = parser.parse_args()
    operation = args.operation

    # Daemon
    logging.basicConfig(filename="foodaemon.log", level=logging.DEBUG)
    daemon = MyDaemon('/Users/marcosaguayo/dev/luracoin/python.pid')

    if operation == 'start':
        print("Starting daemon")
        daemon.start()
        pid = daemon.get_pid()

        if not pid:
            print("Unable run daemon")
        else:
            print("Daemon is running [PID=%d]" % pid)

    elif operation == 'stop':
        print("Stoping daemon")
        daemon.stop()

    elif operation == 'restart':
        print("Restarting daemon")
        daemon.restart()
    elif operation == 'status':
        print("Viewing daemon status")
        pid = daemon.get_pid()

        if not pid:
            print("Daemon isn't running ;)")
        else:
            print("Daemon is running [PID=%d]" % pid)
    elif operation == 'printpeople':
        bc = daemon.get_people()
        print(bc.get_list())
    elif operation == 'add1':
        bc = daemon.get_people()
        bc.add_people({"people": "1"})
        print(bc.get_list())
    elif operation == 'add2':
        bc = daemon.get_people()
        bc.add_people({"people": "2"})
        print(bc.get_list())
    elif operation == 'add3':
        bc = daemon.get_people()
        bc.add_people({"people": "3"})
        print(bc.get_list())
    elif operation == 'add4':
        bc = daemon.get_people()
        bc.add_people({"people": "4"})
        print(bc.get_list())

    sys.exit(0)


if __name__ == '__main__':
    main()

people.py

class People:
    def __init__(self):
        self.people_list = []

    def get_list(self):
        return self.people_list

    def add_people(self, people):
        self.people_list.append(people)

我执行以下操作:

$ python3 test.py start 
*Starting daemon* 
$ python3 test.py add1
*[{'people': '1'}]* 
$ python3 test.py add2
*[{'people': '2'}]*

python3 test.py add2应该返回[{'people': '1'},{'people': '2'}]

我认为发生的是每次使用该类时,列表都会重新启动。我已经尝试在守护进程的__init__上初始化类但是不起作用。

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我不明白这在理论上如何运作。修复它需要完全重写。

你设法启动你的守护进程,但那又是什么?你永远不会谈论它。它正在运行,它 可能按预期工作,但你不会用它做任何事情。

当您使用参数add1调用test.py时,它会创建一个 new 守护程序类(但它不会分叉并启动另一个后台进程,因为您不会在其上调用start())新的数据结构。这意味着一个空的人员列表。然后添加到此列表,打印结果并退出。当您的流程退出时,包含一个条目的人员列表将会消失。守护进程中的人员列表始终为空,因为守护进程只是坐在那里,等待无限循环并打印日志消息。

相比之下,你的stop命令确实有效,因为它只是向正在运行的进程发送信号并将其杀死。

我无法看到MyDaemon类的新实例在某种程度上可以找到是否已经运行了守护进程,然后与之通信的任何证据。

解决这个问题比我有时间做的更多。你需要一种沟通机制。套接字可以,或者你可以使用ZMQ。或管道,但是你需要两个,因为你需要回复。您可以从当前代码中使用启动和停止守护程序的部分。

当您实例化MyDaemon类时,您将检查是否有守护进程正在运行。如果没有,它会启动它。守护进程启动时,它开始监听通信通道。它不会在while True循环中执行任何操作,而是检查是否有新请求要求它实际执行某些操作。

如果您的守护程序已在运行,并且您只想添加到列表或查询其中的内容,则根本不需要MyDaemon类的实例。您可以将请求写入套接字,然后等待守护程序的响应。写一个这样的例子比我有时间花,但我希望这能让你知道它出错的地方以及如何修复它。

或者不要自己修复它并安装redis服务器。它将是一个内存中的键/值存储,它可能适合您的目的。