2010-11-18 16 views
1

私はPythonのために書かれたtwistdライブラリでアプリケーションを書こうとしています。Pythonのツイストアプリケーションの終了

工場= protocol.ServerFactory()

factory.protocol = EchoServer

アプリケーション= service.Application( "エコー")

internet.TCPServer(:アプリケーションファイルは、次のように終わります8001、factory).setServiceParent(アプリケーション)

私のアプリケーションが終了する前に何かを実行したい(ファイルを閉じるなど)。誰もそれを行う方法を知っていますか?これはイベントハンドラであり、クリーンアップ関数がどこから呼び出されたのかわかりません。

答えて

1

ServicestartServicestopServiceのメソッドにコードを追加する必要があります。

from twisted.application import service 
from twisted.internet import protocol 

class MyService(service.Service): 
    def __init__(self,port=8001): 
    self.port = port 
    def startService(self): 
    self.factory = protocol.ServerFactory() 
    self.factory.protocol = EchoServer 
    from twisted.internet import reactor 
    reactor.callWhenRunning(self.startListening) 
    def startListening(self): 
    from twisted.internet import reactor 
    self.listener = reactor.listenTCP(self.port,self.factory) 
    print "Started listening" 
    def stopService(self): 
    self.listener.stopListening() 
    # Any other tidying 


application = service.Application("Echo") 
MyService().setServiceParent(application) 

一つの方法は、このようなものになるだろう

関連する問題