在一个 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 可以解决问题,但不幸的是没有。
最佳答案
server.serve()
中的代码块是设计的,跨越 Thrift 支持的所有目标语言。通常的用例是像这样运行服务器(伪代码):
init server
setup thrift protocol/tramsport stack
server.serve()
shutdown code
“非阻塞”不是指 server.serve()
调用,而是指接受实际客户端调用的代码。使用 TSimpleServer
,服务器一次只能处理一个调用。相比之下,TNonblockingServer
是 designed to accept a number of connections in parallel .
结论:如果你想运行一个 Thrift 服务器,同时还有其他一些工作要做,或者需要在程序运行期间动态地启动和停止服务器,你将需要另一个线程来实现。
关于python - 非阻塞服务器 Apache Thrift Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533973/