{"id":20619715,"url":"https://github.com/twtrubiks/grpc_python_tutorial","last_synced_at":"2025-03-06T20:14:07.092Z","repository":{"id":197352462,"uuid":"698481869","full_name":"twtrubiks/grpc_python_tutorial","owner":"twtrubiks","description":"grpc python 教學  Google RPC","archived":false,"fork":false,"pushed_at":"2023-10-04T14:02:24.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T05:09:08.839Z","etag":null,"topics":["grpc","python3","tutorial"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-30T03:58:16.000Z","updated_at":"2023-11-06T07:10:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"d8d26c1d-23cf-4471-9a61-581f64d0d5aa","html_url":"https://github.com/twtrubiks/grpc_python_tutorial","commit_stats":null,"previous_names":["twtrubiks/grpc_python_tutorial"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fgrpc_python_tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fgrpc_python_tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fgrpc_python_tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fgrpc_python_tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/grpc_python_tutorial/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242277684,"owners_count":20101542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grpc","python3","tutorial"],"created_at":"2024-11-16T12:12:21.538Z","updated_at":"2025-03-06T20:14:07.065Z","avatar_url":"https://github.com/twtrubiks.png","language":"Python","readme":"# grpc python 教學\n\n紀錄 gRPC 也可以稱 Google RPC, 今天來用 python 把玩😀\n\n## 介紹\n\n官網架構圖\n\n簡單說, RPC 就像你 call 本機的 function,\n\n然後這邊就只是你要先去 stub remote,\n\n接著 call function\n\n![alt tag](https://i.imgur.com/BeHImgV.png)\n\n## 好處以及適合使用時機\n\n- 支援  HTTP/2 效能更好\n\n- Protocol Buffers 輕量, 本身就可以當作文件, 不用另外寫文件\n\n- 自帶簡單的驗證\n\n- 跨語法開發\n\n- 適合使用在微服務(microservices)內部溝通\n\n## 環境安裝\n\n```cmd\npip install grpcio grpcio-tools grpc-interceptor\n```\n\n## 定義 proto\n\n很重要, 他就是文檔\n\n[protos/user.proto](protos/user.proto)\n\n## 編譯\n\n範例\n\n``` cmd\npython -m grpc_tools.protoc -I./protos --python_out=. --pyi_out=. --grpc_python_out=. ./protos/user.proto\n```\n\n`--python_out=.` `--pyi_out=.` `--grpc_python_out=.`\n\n這裡的意思都是放在當前目錄底下,\n\n但我們統一把編譯出來的放到 `service_protos` 資料夾底下\n\n```cmd\npython -m grpc_tools.protoc -I./protos --python_out=./service_protos --pyi_out=./service_protos --grpc_python_out=./service_protos ./protos/user.proto\n```\n\n這邊會有 import 問題, 請將 `user_pb2_grpc.py` 第5行修改,\n\n原本\n\n```python\nimport user_pb2 as user__pb2\n```\n\n改成以下任一種你喜歡的\n\n```python\nimport service_protos.user_pb2 as user__pb2\n\n# or\n# from service_protos import user_pb2 as user__pb2\n\n# or\n# from . import user_pb2 as user__pb2\n```\n\n如果你不想修改, 建議就是 `server.py` 和 `clint.py` 放在同一層\n\n## grpc server\n\n✍ server 和 client 都需要編譯後的檔案.\n\n先開啟一個 terminal 執行 server\n\n```cmd\npython3 server.py\n```\n\n## grpc client\n\n✍ server 和 client 都需要編譯後的檔案.\n\n再開啟一個 terminal 執行 client\n\n```cmd\n❯ python3 client.py\ndetails {\n  id: 2\n  value: \"twtrubiks\"\n}\ndetails {\n  id: 2\n  value: \"twtrubiks\"\n}\n```\n\n## grpc interceptor\n\n幫助你更好的管理 grpc log\n\n```cmd\npip install grpc-interceptor\n```\n\n在 `server.py` 中, 加入底下\n\n```python\ninterceptors = [ExceptionToStatusInterceptor()]\nserver = grpc.server(\n        concurrent.futures.ThreadPoolExecutor(max_workers=10),\n        interceptors=interceptors\n    )\n```\n\n如果你加上這個, 當發生錯誤你會更快的找到問題\n\n加入前\n\n![alt tag](https://i.imgur.com/9rgEXjK.png)\n\n加入後\n\n![alt tag](https://i.imgur.com/82XaPdV.png)\n\n## Reference\n\n* [https://grpc.io/docs/what-is-grpc/introduction/](https://grpc.io/docs/what-is-grpc/introduction/)\n\n* [https://grpc.io/docs/languages/python/basics/](https://grpc.io/docs/languages/python/basics/)\n\n* [https://grpc-interceptor.readthedocs.io/en/latest/](https://grpc-interceptor.readthedocs.io/en/latest/)\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n綠界科技ECPAY ( 不需註冊會員 )\n\n![alt tag](https://payment.ecpay.com.tw/Upload/QRCode/201906/QRCode_672351b8-5ab3-42dd-9c7c-c24c3e6a10a0.png)\n\n[贊助者付款](http://bit.ly/2F7Jrha)\n\n歐付寶 ( 需註冊會員 )\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## 贊助名單\n\n[贊助名單](https://github.com/twtrubiks/Thank-you-for-donate)\n\n## License\n\nMIT license","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fgrpc_python_tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fgrpc_python_tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fgrpc_python_tutorial/lists"}