我写了一个使用twisted应用框架的代理服务器。在它的核心,它使用DHT来解决问题。DHT客户机需要几秒钟的时间才能启动,所以我希望确保代理只在DHT准备好之后才接受连接。在
# there is a class like
class EntangledDHT(object):
# connects to the dht
# create the client
dht = EntangledDHT.from_config(config)
# when it can be used this deferred fires
# i want to wait for this before creating the "real" application
dht.ready
# the proxy server, it uses the dht client
port = config.getint(section, 'port')
p = CosipProxy(host=config.get(section, 'listen'),
port=port,
dht=dht,
domain=config.get(section, 'domain'))
## for twistd
application = service.Application('cosip')
serv = internet.UDPServer(port, p)
serv.setServiceParent(service.IService(application))
如何将EntangledDHT
转换为某种Twisted将在启动CosipProxy
服务之前等待的服务?twisted有没有什么机制可以帮我呢?或者我必须添加一个回调函数来创建应用程序的其余部分?谢谢
不要马上打电话给
serv.setServiceParent(service.IService(application))
。相反,请等待在回调中调用它dht.ready
。如果应用程序服务已经在运行,这将导致它被启动。在而且,看起来}将触发,这就是它的样子)。您的插件或tac文件应该是构造服务,而不是启动服务。在调用第一个
dht
本身不是IService
。它应该是;或者更确切地说,调用from_config
的东西应该是一个服务,因为显然from_config
将启动一些连接(至少,在本例中,如果{startService
之前,不会发生任何事情。在相关问题 更多 >
编程相关推荐