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()
相关推荐
python通过thrift访问hbase.docx
一个java和python的thrift例子,其中java作为服务端,python作为客户端
最新的thrift2协议python链接hbase代码,附代码片段,get,put,delete...
Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码
python3通过thrift连接hbase模块修改版,其中hbase-python3,里面的path,根据实际实际路径修改.
资源分类:Python库 所属语言:Python 资源全名:ctec_thrift_client_py3-1.0.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
thrift-client 最简单测试案例
主要介绍了python使用thrift教程的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
python3 使用 thrift 操作hbase 安装hbase-thirft后有一个Hbase报错 使用这个修改完成的Hbase类替换掉原来的Hbase类问题全部解决 主要是因为python版本兼容性带来的问题
博客配套文件,详细演示了python通过thrift2接口查询hbase的方法。
unity3d-thrift-twistedunity3d-thrift-twistedunity3d-thrift-twisted
flume通过thrift协议收集日志-Python。博客地址:http://blog.csdn.net/wangshuwei5/article/details/48902757
php thrift 客户端调用 python服务端,学习了解thrif非常有用附代码和centos thift 安装文件
thrift生成好的python hbase库,配合thrift库可直接操作hbase
hbase thrift python gen-py
1、首先确保hbase安装测试成功,再者确认下hbase的thrift服务是否启动,注意目前的Hbase(本文基于版本0.98.17)有两套thrift接口thrift和thrift2,本文使用thrift,启动命令:hbase thrift -p 9090 start,确保该...
python连接hbase需要用到hbase-thirft类库,但是hbase-thirft只在python2下能正常使用,如果在python3下,会报错...这几个都是python3和python2语法不兼容导致的,下载的包已经把这些错误结果了,在python下能正常使用
主要介绍了python thrift 实现 单端口多服务的过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下