我試圖運行使用python-daemon庫的守護進程。我也正在使用扭曲的網絡。如何以扭曲的形式運行python-daemon
的服務器是非常簡單的:
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()
然後連接將被正確建立和工作。但在這種情況下,它不再是守護進程。
缺少什麼我在這裏?我怎樣才能實現它?