【问题标题】:Non-Blocking Server Apache Thrift Python非阻塞服务器 Apache Thrift Python
【发布时间】:2016-07-24 00:46:18
【问题描述】:

在一个 Python 模块 A 中,我正在做一些事情。在做这些事情的过程中,我正在创建一个 Thrift 连接。问题是在连接开始后,程序卡在网络逻辑中。 (即阻塞)。

在模块 A 中我有:

stuff = "do some stuff"
network.ConnectionManager(host, port, ...)
stuff = "do more stuff" # not getting to this point

在网络中...

ConnectionManager.start_service_handler()
def start_service_handler(self):
        handler = ServiceHandler(self)
        processor = Service.Processor(handler)
        transport = TSocket.TServerSocket(port=self.port)
        tfactory = TTransport.TBufferedTransportFactory()
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        # server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        server = TNonblockingServer(processor, transport, tfactory, pfactory)
        logger().info('starting server...')
        server.serve()

我尝试了这个,但是模块 A 中的代码在连接代码启动后并没有继续。

我认为 TNonblockingServer 可以解决问题,但不幸的是没有。

【问题讨论】:

    标签: python multithreading apache thrift nonblocking


    【解决方案1】:

    server.serve() 处的代码块是经过设计的,涵盖 Thrift 支持的所有目标语言。通常的用例是运行这样的服务器(伪代码):

    init server
    setup thrift protocol/tramsport stack
    server.serve()
    shutdown code
    

    “非阻塞”不是指server.serve() 调用,而是指进行实际客户端调用的代码。使用TSimpleServer,服务器一次只能处理一个呼叫。相比之下,TNonblockingServerdesigned to accept a number of connections in parallel

    结论:如果你想运行一个 Thrift 服务器,同时还有一些其他工作要并行完成,或者需要在程序运行期间动态启动和停止服务器,你将需要另一个线程来实现。

    【讨论】: