我正在尝试使用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()那么连接将被正确地建立和工作。但在本例中,它不再是一个守护进程。
这里我漏掉了什么?我怎样才能做到这一点?
发布于 2016-07-20 21:56:51
Twisted已经内置了守护功能,所以你不需要添加python-daemon。在这两者之间可能有一些有趣的行为重叠,这可能会咬你一口。正如你所看到的,一旦你得到了你的应用程序,就很容易像上面那样在前台运行。将其作为守护进程运行也非常容易;有关twistd的更多信息,请参阅twistd description和twistd man page,但基本上您只需添加几行样板并通过twistd运行您的服务器。
有关如何执行此操作的分步演练,请参阅文章Running a Twisted Perspective Broker example with twistd。
https://stackoverflow.com/questions/38479280
复制相似问题