Reputation: 707
I am trying to run a daemon process using python-daemon library. I am also using twisted for networking.
The server is pretty simple:
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()
And the client which is also supposed to be the daemon process follows as:
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()
When I run the client as python test.py start
the daemon process is started but somehow the connection is not established.
But if I changed the last lines in the client as below:
app = App()
app.run()
Then the connection would be correctly established and working. But in this case it is not a daemon process anymore.
What am I missing here? How can I achieve it?
Upvotes: 0
Views: 957
Reputation: 861
Twisted has daemonizing capabilities built-in already, so you don't need to add python-daemon
. There may be some funny behavior overlaps between the two that may be biting you. As you've seen, once you've gotten your application it's pretty easy to run in the foreground as you've done above. It's also pretty easy to run it as a daemon; see the twistd
description and twistd
man page for more info on twistd
, but basically you're just going to add a few lines of boilerplate and run your server through twistd
.
See the article Running a Twisted Perspective Broker example with twistd for a step-by-step walkthrough of how to do it.
Upvotes: 2