RPC 的 Python 实现 ToRPC

简介:

ToRPC 详细介绍

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

image

这个基准测试中,Python循环10w次,PyPy循环50w次,然后运行3次, 结果在gist:benchmark_result.txt

文章转载自 开源中国社区[https://www.oschina.net]

相关文章
Python print() 打印两个 list ,实现中间换行
Python print() 打印两个 list ,实现中间换行
|
Python
Python实现因子分析(附案例实战)
Python实现因子分析(附案例实战)
1052 0
Python实现因子分析(附案例实战)
|
JSON 区块链 数据格式
Python实现一个简单的区块链
本文介绍如何用Python实现一个简单的区块链。
483 0
|
存储 数据安全/隐私保护 计算机视觉
python 实现pacs功能 推送下拉影像
python 实现dcmtk关联pacs功能 推送下拉影像
229 0
python 实现pacs功能 推送下拉影像
|
算法 大数据 Python
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
123 2
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
|
前端开发 Python
Leecode加法题目3个 每日练习 Python实现
Leecode加法题目3个 每日练习 Python实现
87 0
Leecode加法题目3个 每日练习 Python实现
|
iOS开发 Python
Python实现微信消息连续发送
Python实现微信消息连续发送
Python实现微信消息连续发送
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
|
Python
Python分分钟实现图书管理系统(含代码)
Python分分钟实现图书管理系统(含代码)
286 0
|
JSON 算法 数据安全/隐私保护
Python:使用PyJWT实现JSON Web Tokens加密解密
Python:使用PyJWT实现JSON Web Tokens加密解密
247 0