RPC 的 Python 實現:ToRPC

jopen 9年前發布 | 20K 次閱讀 ToRPC Python開發

ToRPC(Tornado + RPC) 是一個的基于 Tornado IOLoop 的異步TCP和雙向通信的RPC的Python實現。ToRPC非常輕量級,性能優秀(尤其是在PyPy環境下)。

注意:目前為止,ToRPC只在CPython 2.7+和PyPy 2.5+上測試過。

示例

RPC 服務器

from tornado import ioloop
from torpc import RPCServer
server = RPCServer(('127.0.0.1', 5000))

@server.service.register()
def echo(x):
    return x

server.start()
ioloop.IOLoop.instance().start()

RPC 客戶端

from tornado import ioloop, gen
from torpc import RPCClient

def result_callback(f):
    print(f.result())

@gen.coroutine
def using_gen_style():
    want_to_say = 'way to explore'
    ret = yield rc.call('echo', want_to_say)
    assert ret == want_to_say
    print('gen_style complete')

rc = RPCClient(('127.0.0.1', 5000))
rc.call('echo', 'hello world', callback=result_callback)
future = rc.call('echo', 'code for fun')
future.add_done_callback(result_callback)
using_gen_style()
ioloop.IOLoop.instance().start()

更多請瀏覽examples

Performance

系統: CentOS 6.6 x64

處理器: Intel i5-3470 3.20GHz

內存: 8 GB 1600 MHz DDR3

Python: 2.7.10 PyPy: 4.0.0

environment call coroutine(qps) callback(qps)
Python(with timeout) 9842 11614
Python 13192 16638
PyPy(with timeout) 40486 41225
PyPy 53252 59151
PyPy(unix domain) 67100 74362

這個基準測試中,Python循環10w次,PyPy循環50w次,然后運行3次, 結果在gist:benchmark_result.txt


項目主頁:
http://www.baiduhome.net/lib/view/home/1447721979447

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!