等待twisted服务启动后再启动另一个服务

2024-09-30 01:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个使用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有没有什么机制可以帮我呢?或者我必须添加一个回调函数来创建应用程序的其余部分?谢谢


Tags: thetoclientconfigforapplicationporttwisted
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:07

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

而且,看起来dht本身不是IService。它应该是;或者更确切地说,调用from_config的东西应该是一个服务,因为显然from_config将启动一些连接(至少,在本例中,如果{}将触发,这就是它的样子)。您的插件或tac文件应该是构造服务,而不是启动服务。在调用第一个startService之前,不会发生任何事情。在

相关问题 更多 >

    热门问题