ToRPCRPC 的 Python 實(shí)現(xiàn)
ToRPC(Tornado + RPC) 是一個(gè)的基于 Tornado IOLoop 的異步TCP和雙向通信的RPC的Python實(shí)現(xiàn)。ToRPC非常輕量級,性能優(yōu)秀(尤其是在PyPy環(huán)境下)。
注意:目前為止,ToRPC只在CPython 2.7+和PyPy 2.5+上測試過。
示例
RPC 服務(wù)器
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
系統(tǒng): CentOS 6.6 x64
處理器: Intel i5-3470 3.20GHz
內(nèi)存: 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 |
這個(gè)基準(zhǔn)測試中,Python循環(huán)10w次,PyPy循環(huán)50w次,然后運(yùn)行3次, 結(jié)果在gist:benchmark_result.txt
評論
圖片
表情
