【问题标题】:Twisted makeService() async扭曲的 makeService() 异步
【发布时间】:2016-02-16 09:17:20
【问题描述】:

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

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

app/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)

twisted/plugins/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 函数中使用,有人可以帮助我吗?

【问题讨论】:

    标签: python asynchronous plugins twisted


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2016-08-20
      相关资源
      最近更新 更多