Thrift python常见错误分析

10 篇文章 0 订阅
8 篇文章 0 订阅

使用Thrift经常碰到错误提示“No handlers could be found for logger "thrift.server.TServer"”,这句话其实很坑人,尤其是使用thrift的新手。因为,看到这句话也许会认为,应该是自己编写的server程序框架有问题。也就是如下部分:

<span style="font-size:12px;">handler = XXFilterSpamTextServiceHandler()
processor = XXFilterSpamTextService.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "starting serve"
server.serve()
print "done!"</span>
当然,有在这部分出错的,也有在其他地方出错的。下面我们详细分析。

一.程序框架出错

此类错误易犯人群:小白、程序刚刚写完第一次调试阶段。这个错误解法相当简单。比照模版看看出错在那里,官网模版地址http://thrift.apache.org/tutorial/py。server程序代码如下:

import sys, glob
sys.path.append('gen-py')
sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])

from tutorial import Calculator
from tutorial.ttypes import *

from shared.ttypes import SharedStruct

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

class CalculatorHandler:
  def __init__(self):
    self.log = {}

  def ping(self):
    print 'ping()'

  def add(self, n1, n2):
    print 'add(%d,%d)' % (n1, n2)
    return n1+n2

  def calculate(self, logid, work):
    print 'calculate(%d, %r)' % (logid, work)

    if work.op == Operation.ADD:
      val = work.num1 + work.num2
    elif work.op == Operation.SUBTRACT:
      val = work.num1 - work.num2
    elif work.op == Operation.MULTIPLY:
      val = work.num1 * work.num2
    elif work.op == Operation.DIVIDE:
      if work.num2 == 0:
        x = InvalidOperation()
        x.what = work.op
        x.why = 'Cannot divide by 0'
        raise x
      val = work.num1 / work.num2
    else:
      x = InvalidOperation()
      x.what = work.op
      x.why = 'Invalid operation'
      raise x

    log = SharedStruct()
    log.key = logid
    log.value = '%d' % (val)
    self.log[logid] = log

    return val

  def getStruct(self, key):
    print 'getStruct(%d)' % (key)
    return self.log[key]

  def zip(self):
    print 'zip()'

handler = CalculatorHandler()
processor = Calculator.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

# You could do one of these for a multithreaded server
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

print 'Starting the server...'
server.serve()
print 'done.'
接下来,我们需要说的是:CalculatorHandler,这个类名需要根据实际情况构造,比如这里他们的名字thrift中的服务类的名字Calculator,然后加上一个Handler就可以构造为服务类了,这种类我们统称为Handler类。

二.编写的Handler类中小错误

thrift框架做的很好,封装的也很好,不仅仅有代码生成功能,而且还有错误处理机制。这种错误处理机制处理过程是:当Handler类中的出现错误时,一律提示:“No handlers could be found for logger "thrift.server.TServer"”。换句话说,当变量拼写错误时,不会提示某个变量未命名,而提示“No handlers could be found for logger "thrift.server.TServer"”,这样无疑增加了改错难度,坑啊!!!
对于此,比较好的解决方案是:使用try....except  Exception as e:模块来手动打印错误。try模块代码示例如下:
try:
    a=b
except Exception as  e:
    print e
这样出错时候,就会报出哪里出错了!!!!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thrift is a framework for building cross-language services. It allows you to define data types and service interfaces in a simple definition file, and then generate code in various languages to implement those interfaces. In Python, you can use the Thrift library to create both client and server applications. To use Thrift in Python, you first need to install the Thrift library. You can do this using pip: ``` pip install thrift ``` Once installed, you can start using Thrift in your Python code. Here's a simple example to get you started: 1. Define your data types and service interface in a Thrift IDL file (e.g., `example.thrift`): ``` namespace py example struct MyStruct { 1: required string name 2: optional i32 age } service MyService { MyStruct getStruct(1: string id) } ``` 2. Generate Python code from the IDL file using the `thrift` compiler: ``` thrift --gen py example.thrift ``` 3. Implement the service interface in Python: ```python from example import MyService, ttypes class MyHandler: def getStruct(self, id): # Implementation code goes here return ttypes.MyStruct(name="John", age=25) handler = MyHandler() processor = MyService.Processor(handler) # Run the server transport = TSocket.TServerSocket(port=9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() ``` 4. Create a client to interact with the server: ```python from example import MyService, ttypes transport = TSocket.TSocket("localhost", 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MyService.Client(protocol) transport.open() struct = client.getStruct("123") print(struct.name) print(struct.age) transport.close() ``` This is just a basic example to give you an idea of how to use Thrift with Python. You can find more details and advanced usage in the Thrift documentation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值