非阻塞服务器Apache Thrift Python

时间:2016-07-22 19:25:50

标签: python multithreading apache thrift nonblocking

在一个Python模块中,我正在做一些事情。在做这些事情的过程中,我正在创建一个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可以解决这个问题,但遗憾的是没有。

1 个答案:

答案 0 :(得分:3)

init server setup thrift protocol/tramsport stack server.serve() shutdown code 处的代码块是设计的,跨越Thrift支持的所有目标语言。通常的用例是运行这样的服务器(伪代码):

server.serve()

" nonblocking"不是指TSimpleServer电话,而是指接受实际客户电话的代码。使用TNonblockingServer,服务器一次只能处理一个呼叫。相比之下,{{1}}为designed to accept a number of connections in parallel

结论:如果您想要运行Thrift服务器并且还要进行其他一些并行工作,或者需要在程序运行期间动态启动和停止服务器,则需要另一个线程来实现。