ToRPC(Tornado+RPC)是一个的基于TornadoIOLoop的异步TCP和双向通信的RPC的Python实现。ToRPC非常轻量级,性能优秀(尤其是在PyPy环境下)。
注意:目前为止,ToRPC只在CPython2.7+和PyPy2.5+上测试过。
示例RPC服务器
fromtornadoimportioloopfromtorpcimportRPCServerserver=RPCServer(('127.0.0.1',5000))@server.service.register()defecho(x):returnxserver.start()ioloop.IOLoop.instance().start()RPC客户端
fromtornadoimportioloop,genfromtorpcimportRPCClientdefresult_callback(f):print(f.result())@gen.coroutinedefusing_gen_style():want_to_say='waytoexplore'ret=yieldrc.call('echo',want_to_say)assertret==want_to_sayprint('gen_stylecomplete')rc=RPCClient(('127.0.0.1',5000))rc.call('echo','helloworld',callback=result_callback)future=rc.call('echo','codeforfun')future.add_done_callback(result_callback)using_gen_style()ioloop.IOLoop.instance().start()更多请浏览examples。
Performance系统:CentOS6.6x64
处理器:Inteli5-34703.20GHz
内存:8GB1600MHzDDR3
Python:2.7.10PyPy:4.0.0
environmentcallcoroutine(qps)callback(qps)Python(withtimeout)984211614Python1319216638PyPy(withtimeout)4048641225PyPy5325259151PyPy(unixdomain)6710074362这个基准测试中,Python循环10w次,PyPy循环50w次,然后运行3次,结果在gist:benchmark_result.txt
评论