twistedのプロセスとして起動させてバックグランドで実行できるようにする。そうしないとターミナルをずっと立ち上げたままにしておかないといけないのでそうせざるをえないというか。それを可能にさせるのがtwistdまたはtwistd3。twistdで実行するためのpythonファイルの起動部分は下記のような感じ。
from twisted.application import service, internet -略- log.startLogging(sys.stdout) factory = MyFactory() factory.protocol = MyServerProtocol server = internet.TCPServer(9000, factory) application = service.Application("my_httpd") server.setServiceParent(application)
普通の場合のtwistedの起動は下記のような感じ。多少違いがある。
-略- if __name__ == '__main__': log.startLogging(sys.stdout) factory = MyFactory() factory.protocol = MyServerProtocol reactor.listenTCP(9000, factory) reactor.run()
起動するときのコマンドと引数は
# /usr/bin/twistd3 -l /var/log/server.log -y /home/nakahira/twisted/test-server-d.py
という感じで実行するとプロセスとしてバックグラウンドとして実行される。公式ドキュメントではアプリケーションとして実行するという風な解説があったが詳しくはよくわからない。実行したtwistedのプログラムを止めるときはpsコマンドで当該のプロセスを探して直接killするようにしている(私の場合)