python - 非阻塞服务器 Apache Thrift Python

标签 python multithreading apache thrift nonblocking

在一个 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,服务器一次只能处理一个调用。相比之下,TNonblockingServerdesigned to accept a number of connections in parallel .

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

关于python - 非阻塞服务器 Apache Thrift Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533973/

相关文章:

python - 用于从 s3 存储桶下载最新文件的 Boto 脚本

python - 检查一个数组中的所有行是否都存在于另一个更大的数组中

c# - 从 GTK#Thread 访问 System.Drawing.Bitmap 抛出 Object Currently in use elshere 异常

xml - 如何在Spring XML中为Apache Camel的FlexibleAggregationStrategy设置“选择”表达式?

Apache 使用 http/1.0 响应,即使请求是 http/1.1

php - 实现 IMAP 服务器

python - 如何在python中的多个字典列表中查找项目的累积总和

java - 为什么我们不应该在借用的线程上安排中断?

c++ - 线程基本..需要帮助

apache - 需要拒绝除我之外的所有 IP 访问站点并显示友好错误