Thrift python常见错误分析

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

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!"
当然,有在这部分出错的,也有在其他地方出错的。下面我们详细分析。

一.程序框架出错

此类错误易犯人群:小白、程序刚刚写完第一次调试阶段。这个错误解法相当简单。比照模版看看出错在那里,官网模版地址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
这样出错时候,就会报出哪里出错了!!!!



你可能感兴趣的:(注意事项,Linux,做实验)