https://github.com/twtrubiks/grpc_python_tutorial
grpc python 教學 Google RPC
https://github.com/twtrubiks/grpc_python_tutorial
grpc python3 tutorial
Last synced: 11 months ago
JSON representation
grpc python 教學 Google RPC
- Host: GitHub
- URL: https://github.com/twtrubiks/grpc_python_tutorial
- Owner: twtrubiks
- Created: 2023-09-30T03:58:16.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-04T14:02:24.000Z (over 2 years ago)
- Last Synced: 2025-01-17T05:09:08.839Z (about 1 year ago)
- Topics: grpc, python3, tutorial
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# grpc python 教學
紀錄 gRPC 也可以稱 Google RPC, 今天來用 python 把玩😀
## 介紹
官網架構圖
簡單說, RPC 就像你 call 本機的 function,
然後這邊就只是你要先去 stub remote,
接著 call function

## 好處以及適合使用時機
- 支援 HTTP/2 效能更好
- Protocol Buffers 輕量, 本身就可以當作文件, 不用另外寫文件
- 自帶簡單的驗證
- 跨語法開發
- 適合使用在微服務(microservices)內部溝通
## 環境安裝
```cmd
pip install grpcio grpcio-tools grpc-interceptor
```
## 定義 proto
很重要, 他就是文檔
[protos/user.proto](protos/user.proto)
## 編譯
範例
``` cmd
python -m grpc_tools.protoc -I./protos --python_out=. --pyi_out=. --grpc_python_out=. ./protos/user.proto
```
`--python_out=.` `--pyi_out=.` `--grpc_python_out=.`
這裡的意思都是放在當前目錄底下,
但我們統一把編譯出來的放到 `service_protos` 資料夾底下
```cmd
python -m grpc_tools.protoc -I./protos --python_out=./service_protos --pyi_out=./service_protos --grpc_python_out=./service_protos ./protos/user.proto
```
這邊會有 import 問題, 請將 `user_pb2_grpc.py` 第5行修改,
原本
```python
import user_pb2 as user__pb2
```
改成以下任一種你喜歡的
```python
import service_protos.user_pb2 as user__pb2
# or
# from service_protos import user_pb2 as user__pb2
# or
# from . import user_pb2 as user__pb2
```
如果你不想修改, 建議就是 `server.py` 和 `clint.py` 放在同一層
## grpc server
✍ server 和 client 都需要編譯後的檔案.
先開啟一個 terminal 執行 server
```cmd
python3 server.py
```
## grpc client
✍ server 和 client 都需要編譯後的檔案.
再開啟一個 terminal 執行 client
```cmd
❯ python3 client.py
details {
id: 2
value: "twtrubiks"
}
details {
id: 2
value: "twtrubiks"
}
```
## grpc interceptor
幫助你更好的管理 grpc log
```cmd
pip install grpc-interceptor
```
在 `server.py` 中, 加入底下
```python
interceptors = [ExceptionToStatusInterceptor()]
server = grpc.server(
concurrent.futures.ThreadPoolExecutor(max_workers=10),
interceptors=interceptors
)
```
如果你加上這個, 當發生錯誤你會更快的找到問題
加入前

加入後

## Reference
* [https://grpc.io/docs/what-is-grpc/introduction/](https://grpc.io/docs/what-is-grpc/introduction/)
* [https://grpc.io/docs/languages/python/basics/](https://grpc.io/docs/languages/python/basics/)
* [https://grpc-interceptor.readthedocs.io/en/latest/](https://grpc-interceptor.readthedocs.io/en/latest/)
## Donation
文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡:laughing:
綠界科技ECPAY ( 不需註冊會員 )

[贊助者付款](http://bit.ly/2F7Jrha)
歐付寶 ( 需註冊會員 )

[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)
## 贊助名單
[贊助名單](https://github.com/twtrubiks/Thank-you-for-donate)
## License
MIT license