【发布时间】:2016-07-20 10:53:58
【问题描述】:
我正在尝试使用 python-daemon 库运行一个守护进程。我也在使用 twisted 进行网络连接。
服务器非常简单:
class Echoer(pb.Root):
def remote_echo(self, st):
print 'echoing:', st
return st
if __name__ == '__main__':
serverfactory = pb.PBServerFactory(Echoer())
reactor.listenTCP(8789, serverfactory)
reactor.run()
同样应该是守护进程的客户端如下:
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/null'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
def run(self):
clientfactory = pb.PBClientFactory()
reactor.connectTCP("localhost", 8789, clientfactory)
d = clientfactory.getRootObject()
d.addCallback(self.send_msg)
reactor.run()
def send_msg(self, result):
d = result.callRemote("echo", "hello network")
d.addCallback(self.get_msg)
def get_msg(self, result):
print "server echoed: ", result
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
当我以python test.py start 运行客户端时,守护进程已启动,但不知何故连接未建立。
但如果我将客户端的最后几行更改如下:
app = App()
app.run()
然后连接将正确建立并正常工作。但在这种情况下,它不再是守护进程。
我在这里缺少什么?我怎样才能实现它?
【问题讨论】: