`
younglibin
  • 浏览: 1193016 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用python实现thrift异步client(tornado和twisted)

 
阅读更多

thrift 在python中使用了 tornado和  twisted 来作为异步的webserive服务提供异步接口,自身并没有实现

    twisted:         Generate Twisted-friendly RPC services.

    tornado:         Generate code for use with Tornado.

  在使用tornado的使用,thrift0.9.1这个版本  生成的tornado python文件 和 最新版的  tornado 目录结构有变化,

导致引用的 类找不到,又考虑到如果有版本升级问题,所以放弃修改tornado或者使用低版本tornado,目前改为使用twisted。

 

1.首先 ,thrift  中 生产python文件默认是没有asybcClient这样接口的所有,查看帮组之后使用

thrift -gen py:tornado -out ./ hello.throft 

  子生成文件中我们可以看到Iface 中有很多  callback  的 回调函数

2.创建 异步client

 25     transport = TSocket.TSocket("192.168.1.105", 7911)
 26     transport = TTransport.TFramedTransport(transport)
 27     protocol = TJSONProtocol.TJSONProtocolFactory()
 28     transport.open()
 29     client = Hello.Client(transport, protocol)
 30     client.helloString("sad", callback)

 

3.异步回调函数:

/home/libin/software/thrift/thrift-0.9.1/tutorial/py.tornado/PythonClient.py

22 import sys
 23 import glob
 24 sys.path.append('gen-py.tornado')
 25 sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
 26 
 27 import logging
 28 
 29 from tutorial import Calculator
 30 from tutorial.ttypes import Operation, Work, InvalidOperation
 31 
 32 from thrift import TTornado
 33 from thrift.transport import TSocket
 34 from thrift.transport import TTransport
 35 from thrift.protocol import TBinaryProtocol
 36 
 37 from tornado import gen
 38 from tornado import ioloop
 39 
 40 
 41 @gen.engine
 42 def communicate(callback=None):
 43     # create client
 44     transport = TTornado.TTornadoStreamTransport('localhost', 9090)
 45     pfactory = TBinaryProtocol.TBinaryProtocolFactory()
 46     client = Calculator.Client(transport, pfactory)
 47 
 48     # open the transport, bail on error
 49     try:
 50         yield gen.Task(transport.open)
 51     except TTransport.TTransportException as ex:
 52         logging.error(ex)
 53         if callback:
 54             callback()
 55         return
 56 
 57     # ping
 58     yield gen.Task(client.ping)
 59     print "ping()"
 60 
 61     # add
 62     sum_ = yield gen.Task(client.add, 1, 1)
 63     print "1 + 1 = {}".format(sum_)
 64 
 65     # make a oneway call without a callback (schedule the write and continue
 66     # without blocking)
 67     client.zip()
 68     print "zip() without callback"
 69 
 70     # make a oneway call with a callback (we'll wait for the stream write to
 71     # complete before continuing)
 72     yield gen.Task(client.zip)
 73     print "zip() with callback"
 74 
 75     # calculate 1/0
 76     work = Work()
 77     work.op = Operation.DIVIDE
 78     work.num1 = 1
 79     work.num2 = 0
 80 
 81     try:
 82         quotient = yield gen.Task(client.calculate, 1, work)
 83         print "Whoa? You know how to divide by zero?"
 84     except InvalidOperation as io:
 85         print "InvalidOperation: {}".format(io)
 86 
 87     # calculate 15-10
 88     work.op = Operation.SUBTRACT
 89     work.num1 = 15
 90     work.num2 = 10
 91 
 92     diff = yield gen.Task(client.calculate, 1, work)
 93     print "15 - 10 = {}".format(diff)
 94 
 95     # getStruct
 96     log = yield gen.Task(client.getStruct, 1)
 97     print "Check log: {}".format(log.value)
 98 
 99     # close the transport
100     client._transport.close()
101 
102     if callback:
103         callback()
104 
105 
106 def main():
107     # create an ioloop, do the above, then stop
108     io_loop = ioloop.IOLoop.instance()
109     def this_joint():
110         communicate(callback=io_loop.stop)
111     io_loop.add_callback(this_joint)
112     io_loop.start()
113 
114 
115 if __name__ == "__main__":
116     main()
                                   

 

 

使用 twisted:

thrift -gen py:twisted -out ./ hello.throft 

客户端代码:

 

 22 import sys, glob
 23 sys.path.append('gen-py.twisted')
 24 sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
 25 
 26 from tutorial import Calculator
 27 from tutorial.ttypes import *
 28 
 29 from twisted.internet.defer import inlineCallbacks
 30 from twisted.internet import reactor
 31 from twisted.internet.protocol import ClientCreator
 32 
 33 from thrift import Thrift
 34 from thrift.transport import TTwisted
 35 from thrift.protocol import TBinaryProtocol
 36 
 37 @inlineCallbacks
 38 def main(client):
 39   yield client.ping()
 40   print 'ping()'
 41 
 42   sum = yield client.add(1,1)
 43   print '1+1=%d' % (sum)
 44 
 45   work = Work()
 46 
 47   work.op = Operation.DIVIDE
 48   work.num1 = 1
 49   work.num2 = 0
 50 
 51   try:
 52     quotient = yield client.calculate(1, work)
 53     print 'Whoa? You know how to divide by zero?'
 54   except InvalidOperation, io:
 55     print 'InvalidOperation: %r' % io
 56 
 57   work.op = Operation.SUBTRACT
 58   work.num1 = 15
 59   work.num2 = 10
 60 
 61   diff = yield client.calculate(1, work)
 62   print '15-10=%d' % (diff)
 63 
 64   log = yield client.getStruct(1)
 65   print 'Check log: %s' % (log.value)
 66   reactor.stop()
 67 
 68 if __name__ == '__main__':
 69     d = ClientCreator(reactor,
 70                       TTwisted.ThriftClientProtocol,
 71                       Calculator.Client,
 72                       TBinaryProtocol.TBinaryProtocolFactory(),
 73                       ).connectTCP("127.0.0.1", 9090)
 74     d.addCallback(lambda conn: conn.client)
 75     d.addCallback(main)
 76 
 77     reactor.run()

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics