Python守护进程使用相同的对象列表
我有一个班叫People。这个课程有一个列表。我不想将此列表保存在文件或数据库中,全部保存在内存中,因此我认为可以通过创建守护进程并保持进程打开来工作,下面是我的代码: daemon.pyPython守护进程使用相同的对象列表,python,python-3.x,daemon,python-daemon,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
# 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()
人啊
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'}]
我认为每次我使用这个类时,列表都会重新启动。我尝试在守护进程的\uuuu init\uuuu
上初始化该类,但不起作用
有人知道我该如何解决这个问题吗?我不明白这在理论上是怎么回事。修复它需要完全重写 你成功地启动了你的守护进程,但是接下来呢?你从来不跟它说话。它正在运行,可能会按预期工作,但您不会将其用于任何用途 当您使用参数add1调用test.py时,它会创建一个带有新数据结构的new守护进程类(但它不会分叉并启动另一个后台进程,因为您不会对其调用start())。这意味着一个空的人员列表。然后添加到此列表,打印结果并退出。进程退出时,包含一个条目的人员列表将消失。您的守护进程中的人员列表总是空的,因为守护进程只是坐在那里,在无限循环中等待并打印日志消息 相反,stop命令确实有效,因为它只是向正在运行的进程发送一个信号并终止它 我看不到任何证据表明MyDaemon类的一个新实例会以某种方式发现是否有一个已经运行的守护进程,然后与之通信 解决这个问题我没有时间做。你需要一个沟通机制。套接字就可以了,或者您可以使用ZMQ。或者管道,但是你需要两个,因为你需要得到回复。您将使用当前代码中启动和停止守护进程的部分 当您实例化MyDaemon类时,您将检查是否有守护进程正在运行。如果没有,它会启动它。当守护进程启动时,它开始监听通信通道。它不会在
while True
循环中什么都不做,而是检查是否有新的请求要求它实际执行某些操作
如果您的守护进程已经在运行,并且您只想向列表中添加或查询其中的内容,那么您根本不需要MyDaemon类的实例。而是将请求写入套接字,然后等待守护进程的响应。写一个这样的例子比我花的时间还多,但我希望这能让你知道哪里出了问题以及如何解决
或者不要自己修复,而是安装redis服务器。它将是一个内存中的键/值存储,可能适合您的用途
add_transaction
函数的主体在哪里?@kingJulian很抱歉,这是一个编辑错误。我们也可以看到您的人员类吗?@kingJulian EditedI现在无法在我的机器上运行您的代码,但由于您希望在人员
类的所有对象上使用相同的列表,您可以将人员列表
定义为实例列表,而不是实例列表。这就是将人员列表
移动到初始化
函数之外。