2

我编写了一个使用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))

如何将EntangledDHTTwisted 转换为某种服务,然后再启动CosipProxy服务?扭曲中是否有任何机制可以为我做到这一点?还是我必须添加一个回调来dht.ready创建应用程序的其余部分?谢谢

4

1 回答 1

2

不要serv.setServiceParent(service.IService(application))马上打电话。相反,等待在你的回调中调用它到dht.ready. 如果应用程序服务已经在运行,这将导致它启动。

此外,它看起来不像dht自己是一个IService. 它应该是; 或者更确切地说,调用的东西from_config应该是一个服务,因为显然from_config会启动一些连接(至少,dht.ready在这个例子中,如果要触发的话,它看起来就是这样)。您的插件或 tac 文件应该构建服务,而不是启动服务。startService在调用第一个之前,什么都不会发生。

于 2010-11-18T19:50:06.663 回答