Skip to main content
 首页 » 编程设计

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

2025年01月19日10insus

我有一个名为 People 的类。这个类有一个列表。我不想将此列表保存在文件或数据库中,全部保存在内存中,所以我认为可行的方法是创建一个守护进程并保持进程打开,这是我的代码:

守护程序.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() 

人.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__ 上初始化类,但没有成功。

谁知道我该如何解决这个问题?

请您参考如下方法:

即使在理论上,我也不明白这是怎么回事。修复它需要完全重写。

您设法启动了您的守护进程,但是接下来呢?你永远不会和它说话。它在那里运行,会可能按预期工作,但您不会将它用于任何用途。

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

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

我在任何地方都看不到任何证据表明 MyDaemon 类的新实例会以某种方式发现是否有守护进程已经在运行,然后与之通信。

解决这个问题超出了我的时间。您需要一种通信机制。套接字可以,或者您可以使用 ZMQ。或者管道,但是你需要两个,因为你需要得到响应。您将从当前代码中使用启动和停止守护程序的部分。

当你实例化你的 MyDaemon 类时,你会检查是否有守护进程在运行。如果没有,它将启动它。当守护进程启动时,它开始监听通信 channel 。它不会在 while True 循环中什么也不做,而是检查是否有新请求要求它实际做某事。

如果您的守护进程已经在运行,而您只想添加到列表或查询其中的内容,则根本不需要 MyDaemon 类的实例。相反,您可以将请求写入套接字,然后等待守护进程的响应。写一个这样的例子超出了我的时间,但我希望这能让你知道哪里出了问题以及如何解决它。

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