2016-07-20 5 views
0

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() 

も、デーモンプロセスであるために仮定されているクライアントは、として、以下::

サーバーは非常に簡単ですpython test.py startデーモンプロセスがあるとして、私は、クライアントを実行すると

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() 

何らかの形で接続が確立されていません。

しかし、私は以下のように、クライアントの最後の行を変更した場合:

app = App() 
app.run() 

を次に接続が正常に確立して作業することでしょう。しかし、この場合、もうデーモンプロセスではありません。

私はここで何が欠けていますか?どうすれば達成できますか?

答えて

1

Twistedにはすでにデーモン機能が組み込まれているので、python-daemonを追加する必要はありません。あなたを噛んでいるかもしれない2つの面白い行動の重なりがあるかもしれません。あなたが見たように、アプリケーションを入手すると、前のようにフォアグラウンドで実行するのはかなり簡単です。デーモンとして実行するのもかなり簡単です。 twistdの詳細については、twistd descriptiontwistd man pageを参照してください。基本的には、ほんの数行の定型文を追加し、サーバーをtwistdで実行しています。

手順については、Running a Twisted Perspective Broker example with twistdの手順をご覧ください。

関連する問題