日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 使用grpc

發布時間:2024/3/12 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 使用grpc 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、安裝

pip install grpcio # gprc pip install grpcio-tools # 代碼生成工具 pip install protobuf # 協議

二、創建proto目錄并創建proto文件

# user.proto // 指定版本 syntax="proto3"; // 包 package user;// 創建service service User {// user methodrpc AddUser(UserRequest) returns (UserResponse) {}rpc GetUser(GetUserRequest) returns (GetUserResponse) {} }// 請求參數消息體 1、2 是指參數的個數順序 message UserRequest {string username = 1;int32 age = 2; }// 返回參數消息體 message UserResponse {string err = 1; }// 請求參數消息體 message GetUserRequest {string username = 1; }// 返回參數消息體 message GetUserResponse {string username = 1;int32 age = 2; }

# 生成pb2.py、pb2_grpc.py文件

python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. ./proto/user.proto

# ?--python_out= ?pb2.py 文件路徑
# ?--grpc_python_out= ?pb2_grpc.py文件路徑
# ?./proto/user.proto ?proto文件路徑

注: 這里遇到一個問題?
?? ? 使用 python3 -m grpc_tools.protoc -I=./proto --python_out=./proto --grpc_python_out=./proto ./proto/user.proto, 生成的pb2_grpc.py文件 導入 pb2.py 是 import user_pb2 as user__pb2, 這里會報錯,應該是 from proto import user_pb2 as user__pb2


?? ? 當我使用 ?python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. ./proto/user.proto 生成時,不會有這個問題。導入變成 from proto import user_pb2 as proto_dot_user__pb2

三、服務端代碼

# user.py from proto import user_pb2, user_pb2_grpcclass UserServicer(user_pb2_grpc.UserServicer):def AddUser(self, request, context):print(request.username, request.age)# 具體的業務邏輯return user_pb2.UserResponse(err='ok')def GetUser(self, request, context):print(request.username)# 具體的業務邏輯return user_pb2.GetUserResponse(username='danni', age=12) # server.py import timeimport grpc from concurrent import futuresfrom proto import user_pb2, user_pb2_grpc from moudle.user import UserServicerdef main():# futures.ThreadPoolExecutor(max_workers=10) 指定最大線程數,不指定默認是 cpu個數 x 5"""if max_workers is None:# Use this number because ThreadPoolExecutor is often# used to overlap I/O instead of CPU work.max_workers = (os.cpu_count() or 1) * 5"""server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))# 注冊User Serviceuser_server = UserServicer()user_pb2_grpc.add_UserServicer_to_server(user_server, server)# 啟動服務server.add_insecure_port("127.0.0.1:8000")server.start()try:print('start rpc service')while True:time.sleep(60 * 60)except:server.stop(0)if __name__ == '__main__':main()

四、客戶端代碼

# client.py import grpcfrom proto import user_pb2_grpc, user_pb2def main():# 連接RPC 服務器channel = grpc.insecure_channel("localhost:8000")# 創建 Stubuser_cli = user_pb2_grpc.UserStub(channel)# 調用 AddUservalue = user_cli.AddUser(user_pb2.UserRequest(username='danni', age=20))print(value)if __name__ == '__main__':main()

一個簡單的python gprc使用,便于理解

總結

以上是生活随笔為你收集整理的python 使用grpc的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。