|
| 1 | +# thriftpy2 - Thrift RPC 框架的 |
| 2 | + |
| 3 | +<div class="no-link"> |
| 4 | + |
| 5 | +[](https://travis-ci.com/Thriftpy/thriftpy2) |
| 6 | +[](https://codecov.io/gh/Thriftpy/thriftpy2) |
| 7 | +[](https://pypi.org/project/thriftpy2/) |
| 8 | +[](https://pypi.org/project/thriftpy2/) |
| 9 | +[](https://pypi.org/project/thriftpy2/) |
| 10 | +[](https://pypi.org/project/thriftpy2/) |
| 11 | + |
| 12 | +</div> |
| 13 | + |
| 14 | +[[TOC]] |
| 15 | + |
| 16 | +## 1. 项目简介 |
| 17 | + |
| 18 | +| 项目 | 信息 | |
| 19 | +| ----------- | ---------------------------------------------------------------------------------------------- | |
| 20 | +| 项目地址 | [PyPI](https://pypi.org/project/thriftpy2/) \| [GitHub](https://github.com/Thriftpy/thriftpy2) | |
| 21 | +| 官方文档 | [官方文档](https://thriftpy2.readthedocs.io/en/latest/) | |
| 22 | +| 开源协议 | MIT | |
| 23 | +| Python 版本 | Python 2.7 ~ 3.11 | |
| 24 | +| 标签 | RPC、asyncio | |
| 25 | + |
| 26 | +ThriftPy2 是 Thrift RPC 框架的 Python 实现,它是官方 Thrift Python 包的另一个实现,兼容 `thriftpy`,并且提供了无需编译 `.thrift` 文件即可直接使用的功能。 |
| 27 | + |
| 28 | +ThriftPy2 的速度比 `thriftpy` 快许多,这可以从官方的 [benchmark](https://thriftpy2.readthedocs.io/en/latest/#benchmarks) 中找到。 |
| 29 | + |
| 30 | +如果你需要使用 ThriftPy2,你需要安装 `cython` 和 `thriftpy2`: |
| 31 | + |
| 32 | +```bash |
| 33 | +pip install cython thriftpy2 |
| 34 | +``` |
| 35 | + |
| 36 | +由于 ThriftPy2 兼容 `thriftpy`,所以你可以使用 `thriftpy` 的方式来使用 ThriftPy2: |
| 37 | + |
| 38 | +```python |
| 39 | +import thriftpy2 as thriftpy |
| 40 | +``` |
| 41 | + |
| 42 | +## 2. 快速开始 |
| 43 | + |
| 44 | +下面所使用的 Thrift 文件如下: |
| 45 | + |
| 46 | +```go |
| 47 | +service PingPong { |
| 48 | + string ping(), |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +基本示例: |
| 53 | + |
| 54 | +::: code-tabs |
| 55 | + |
| 56 | +@tab Server |
| 57 | + |
| 58 | +```python :no-line-numbers |
| 59 | +import thriftpy2 |
| 60 | +from thriftpy2.rpc import make_server |
| 61 | + |
| 62 | +pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift") |
| 63 | + |
| 64 | +class Dispatcher(object): |
| 65 | + def ping(self): |
| 66 | + return "pong" |
| 67 | + |
| 68 | +server = make_server(pingpong_thrift.PingPong, Dispatcher(), '127.0.0.1', 6000) |
| 69 | +server.serve() |
| 70 | +``` |
| 71 | + |
| 72 | +@tab Client |
| 73 | + |
| 74 | +```python :no-line-numbers |
| 75 | +import thriftpy2 |
| 76 | +from thriftpy2.rpc import make_client |
| 77 | + |
| 78 | +pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift") |
| 79 | + |
| 80 | +client = make_client(pingpong_thrift.PingPong, '127.0.0.1', 6000) |
| 81 | +print(client.ping()) |
| 82 | +``` |
| 83 | + |
| 84 | +::: |
| 85 | + |
| 86 | +同样支持 `asyncio`,使用上述 Thrift 文件,我们可以这样写: |
| 87 | + |
| 88 | +::: code-tabs |
| 89 | + |
| 90 | +@tab Server |
| 91 | + |
| 92 | +```python :no-line-numbers |
| 93 | +import asyncio |
| 94 | +import thriftpy2 |
| 95 | +from thriftpy2.rpc import make_aio_server |
| 96 | + |
| 97 | +echo_thrift = thriftpy2.load("echo.thrift", module_name="echo_thrift") |
| 98 | + |
| 99 | +class Dispatcher(object): |
| 100 | + async def echo(self, param): |
| 101 | + print(param) |
| 102 | + await asyncio.sleep(0.1) |
| 103 | + return param |
| 104 | + |
| 105 | +def main(): |
| 106 | + server = make_aio_server( |
| 107 | + echo_thrift.EchoService, Dispatcher(), '127.0.0.1', 6000) |
| 108 | + server.serve() |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
| 112 | +``` |
| 113 | + |
| 114 | +@tab Client |
| 115 | + |
| 116 | +```python :no-line-numbers |
| 117 | +import thriftpy2 |
| 118 | +import asyncio |
| 119 | +from thriftpy2.rpc import make_aio_client |
| 120 | + |
| 121 | +echo_thrift = thriftpy2.load("echo.thrift", module_name="echo_thrift") |
| 122 | + |
| 123 | +async def request(): |
| 124 | + client = await make_aio_client( |
| 125 | + echo_thrift.EchoService, '127.0.0.1', 6000) |
| 126 | + print(await client.echo('hello, world')) |
| 127 | + client.close() |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + loop = asyncio.get_event_loop() |
| 131 | + loop.run_until_complete(request()) |
| 132 | +``` |
| 133 | + |
| 134 | +::: |
0 commit comments