一个最小的示例将有助于使您的问题更清楚。然而,根据多年的 Twisted 经验,我有一个有根据的猜测。我想你写了一个这样的程序:
from twisted.internet import endpoints, reactor, protocol
factory = protocol.Factory()
factory.protocol = protocol.Protocol
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
d = endpoint.listen(factory)
def listenFailed(reason):
reactor.stop()
d.addErrback(listenFailed)
reactor.run()
你在正确的轨道上。不幸的是,您有订购问题。reactor.stop
失败的原因ReactorNotRunning
是listen
Deferred 在你调用之前失败了reactor.run
。也就是说,到你做的时候它已经失败了d.addErrback(listenFailed
),所以listenFailed
马上就被调用了。
对此有多种解决方案。一种是编写 .tac 文件并使用服务:
from twisted.internet import endpoints, reactor, protocol
from twisted.application.internet import StreamServerEndpointService
from twisted.application.service import Application
application = Application("Some Kind Of Server")
factory = protocol.Factory()
factory.protocol = protocol.Protocol
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
service = StreamServerEndpointService(endpoint, factory)
service.setServiceParent(application)
这是使用运行的twistd
,例如twistd -y thisfile.tac
另一种选择是使用服务所基于的低级功能reactor.callWhenRunning
:
from twisted.internet import endpoints, reactor, protocol
factory = protocol.Factory()
factory.protocol = protocol.Protocol
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
def listen():
d = endpoint.listen(factory)
def listenFailed(reason):
reactor.stop()
d.addErrback(listenFailed)
reactor.callWhenRunning(listen)
reactor.run()