Twisted makeService()async

时间:2016-02-16 09:17:20

标签: python asynchronous plugins twisted

我需要创建一个MultiService,它在扭曲的插件中异步加载,有没有办法实现呢?

这是我到目前为止所做的:

应用/ plugins.py

class Options(usage.Options):
    synopsis = '[options]'
    longdesc = (
        'Starts app modules services. The modules must '
        'be configured and enabled for the current server '
        'before being started. To see the list installed modules '
        'use the --list switch.')
    optFlags = [['list', 'l', 'Display the list of installed modules.']]


def makeService(options):
    from twisted.internet import reactor
    debug = options['debug']
    return app.ModuleService.load(reactor)

扭曲/插件/ app_plugins.py

TestService = ServiceMaker(
    'test',
    'app.plugins',
    'Test service.',
    'test')

app.py

class ModuleService(service.MultiService):
    def __init__(self, reactor=None):

        # Twisted old style classes
        service.MultiService.__init__(self)

        if reactor is None:
            from twisted.internet import reactor
        self._reactor = reactor

    @classmethod
    @defer.inlineCallbacks
    def load(cls, reactor=None):

        modules = yield get_modules()

        service = cls(reactor)

        for module in modules:
            # module tcp server
            mod_endpoint = endpoints.TCP4ServerEndpoint(reactor, module.port)
            module_service = internet.StreamServerEndpointService(
                mod_endpoint,
                pb.PBServerFactory(spread.RunnerServer()))
            module_service.setServiceParent(service)

        defer.returnValue(service)

所以我的问题是MultiService是异步加载的,因此无法在makeService函数中使用,有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

扭曲的服务不应该异步启动。服务启动在reactoru启动之前完成。您应该更改您的应用程序架构。我目前正在研究如何:D