【发布时间】:2016-11-03 00:06:57
【问题描述】:
我正在使用著名的code referenced here 或here 在 Python 中做一个守护进程,如下所示:
import sys, daemon
class test(daemon.Daemon):
def run(self):
self.db = somedb.connect() # connect to a DB
self.blah = 127
with open('blah0.txt', 'w') as f:
f.write(self.blah)
# doing lots of things here, modifying self.blah
def before_stop(self):
self.db.close() # properly close the DB (sync to disk, etc.)
with open('blah1.txt', 'w') as f:
f.write(self.blah)
daemon = test(pidfile='_.pid')
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.before_stop() # AttributeError: test instance has no attribute 'blah'
daemon.stop()
问题是当调用./myscript.py stop 和daemon.before_stop() 时,不再有对self.blah 的引用!
AttributeError: 测试实例没有属性'blah'
因此,使用这种守护程序方法,在停止守护程序之前无法访问守护程序的变量...
问题:如何访问之前的守护类变量:
停在
./myscript.py stop使用 SIGTERM 停止
(被杀?)
【问题讨论】:
-
我建议检查守护进程对象的引用,看看你是否真的在调用之间访问同一个对象。如果需要,请使用您自己的签名。
-
@dcwhcdhw 这正是问题所在,调用之间的对象不同。关闭守护进程时如何处理?
标签: python linux daemon python-daemon