【问题标题】:twisted deferToThread tcp connections扭曲的 deferToThread tcp 连接
【发布时间】:2016-01-20 08:00:09
【问题描述】:

我是twisted新手,尝试编写我的第一个应用程序,我实际上遇到了这个问题:

我有一个主线程,它加载了一堆要运行的模块,每个模块都位于远程服务器上并通过 tcp 连接可用,这里的模块运行代码如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct

from twisted.internet import endpoints
from twisted.internet.defer import inlineCallbacks
from twisted.spread import pb
from txapp.core import get_modules

from txmod.spread import EnvelopeSpreadReceiver


@inlineCallbacks
def listen(reactor):
    modules = yield get_modules()

    for mod in modules:
        endpoint = endpoints.serverFromString(reactor, 'tcp:%d:%s' %
                                              (int(mod.get('module_server_port') or 0),
                                               socket.inet_ntoa(struct.pack('!L', mod.get('module_server_ipaddr'))),))
        endpoint.listen(pb.PBServerFactory(EnvelopeSpreadReceiver()))
        print 'PBServerFactory starting on %s:%d for module %s' % (
            socket.inet_ntoa(struct.pack('!L', mod.get('module_server_ipaddr'))),
            int(mod.get('module_server_port')),
            mod.get('module_name'))


def main():
    from twisted.internet import reactor
    listen(reactor)
    reactor.run()


if __name__ == "__main__":
    main()

一个给定的服务器可能在不同的端口上运行多个模块,但通常是:1个服务器,1个模块

运行应用程序的主服务器将尝试连接到每个模块,运行代码并期待结果(成功与否)。

我正在努力解决如何使这段代码不阻塞:如果服务器无法响应,tcp 连接可能会挂起,所以我应该将每个模块 tcp 连接放在一个单独的线程上,deferToThread 吗?

主应用程序每天将处理数百万个请求,因此它应该是完全非阻塞的。

这是正确的处理方式吗?有人可以指出如何实现这一目标的正确方向吗?

【问题讨论】:

    标签: python multithreading twisted


    【解决方案1】:

    Twisted 的事件循环 API 不是线程安全的,如 Twisted's threading documentation 所述。这意味着您绝对不能调用deferToThread 来处理连接;如果你尝试它会崩溃。

    等待的 TCP 连接不会导致事件循环卡住;用于向连接发送和接收数据的 API 已经是非阻塞的。所以我认为你这里没有问题要解决,它已经为你解决了。

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多