2010-11-18 10 views
1

파이썬 용으로 작성된 twistd 라이브러리가있는 응용 프로그램을 작성하려고합니다. 응용 프로그램 파일과 같은 종료 다음파이썬에서 twisted 응용 프로그램의 종료

공장 = protocol.ServerFactory()

factory.protocol = EchoServer

응용 프로그램 = service.Application ("에코")

internet.TCPServer (8001, 공장) .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) 
:

한 가지 방법은 다음과 같이 될 것이다

관련 문제