非阻塞服务器Apache Thrift Python

非阻塞服务器Apache Thrift Python,python,multithreading,apache,thrift,nonblocking,Python,Multithreading,Apache,Thrift,Nonblocking,在一个Python模块A中,我正在做一些事情。在做这些事情的过程中,我在创造一种节俭的联系。问题是,连接启动后,程序会卡在网络逻辑中。(即阻塞) 在模块A中,我有: stuff = "do some stuff" network.ConnectionManager(host, port, ...) stuff = "do more stuff" # not getting to this point 在网络中 ConnectionManager.start_service_handler()

在一个Python模块A中,我正在做一些事情。在做这些事情的过程中,我在创造一种节俭的联系。问题是,连接启动后,程序会卡在网络逻辑中。(即阻塞)

在模块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可以做到这一点,但不幸的是没有做到。

server.service()
上的代码块是经过设计的,它跨越了Thrift支持的所有目标语言。通常的用例是运行这样的服务器(伪代码):

“非阻塞”不是指
server.service()
调用,而是指执行实际客户端调用的代码。使用
TSimpleServer
,服务器一次只能处理一个调用。相比之下,
TNonblockingServer

结论:如果您想运行一个Thrift服务器,同时还要做一些其他工作,或者需要在程序运行期间动态地启动和停止服务器,那么需要另一个线程来实现这一点

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