Skip to content

Commit 32027ef

Browse files
committed
docs(pypi-package): add rpc/thriftpy2
1 parent d9fb1ac commit 32027ef

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

docs/.vuepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default defineUserConfig({
2020
head: [
2121
['link', { rel: 'icon', href: `${BASE_PATH}favicon.svg` }]
2222
],
23+
base: BASE_PATH,
2324
markdown: {
2425
code: {
2526
lineNumbers: 10

docs/pypi-package/async/aiofiles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| Python 版本 | Python 3.6 ~ 3.10 |
2121
| 标签 | asyncio |
2222

23-
`aiofiles` 是一个 Apache 2 许可的库,用 Python 编写,用于在 `asyncio` 应用程序中处理本地磁盘文件。
23+
`aiofiles` 是一个 Apache 2 许可的第三方库,用 Python 编写,用于在 `asyncio` 应用程序中处理本地磁盘文件。
2424

2525
```bash
2626
pip install aiofiles

docs/pypi-package/rpc/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python RPC 相关
2+
3+
<AutoCatalog />

docs/pypi-package/rpc/thriftpy2.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# thriftpy2 - Thrift RPC 框架的
2+
3+
<div class="no-link">
4+
5+
[![](https://travis-ci.com/Thriftpy/thriftpy2.svg?branch=develop)](https://travis-ci.com/Thriftpy/thriftpy2)
6+
[![](https://img.shields.io/codecov/c/github/Thriftpy/thriftpy2.svg)](https://codecov.io/gh/Thriftpy/thriftpy2)
7+
[![](https://img.shields.io/pypi/dm/thriftpy2.svg)](https://pypi.org/project/thriftpy2/)
8+
[![](https://img.shields.io/pypi/v/thriftpy2.svg)](https://pypi.org/project/thriftpy2/)
9+
[![](https://img.shields.io/pypi/pyversions/thriftpy2.svg)](https://pypi.org/project/thriftpy2/)
10+
[![](https://img.shields.io/pypi/implementation/thriftpy2.svg)](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

Comments
 (0)