最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python golang中g(shù)rpc 使用示例代碼詳解

 更新時間:2020年06月03日 09:01:40   作者:hw_owen ·  
這篇文章主要介紹了python golang中g(shù)rpc 使用,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

python

1、使用前準(zhǔn)備,安裝這三個庫

pip install grpcio
pip install protobuf
pip install grpcio_tools

2、建立一個proto文件hello.proto

// [python quickstart](https://grpc.io/docs/quickstart/python.html#run-a-grpc-application)
// python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto

// helloworld.proto
syntax = "proto3";
package test;

service Greeter {
 rpc SayHello(HelloRequest) returns (HelloReply) {}
 rpc SayHelloAgain(HelloRequest) returns (HelloReply) {}

}
service Greetera{
 rpc SayStudent(Studentid) returns (Student){}
}
message Student {
 string msg=1;//json
}


message Studentid{
 string id=1;
}
message HelloRequest {
 string name = 1;
}

message HelloReply {
 string message = 1;
}

3、執(zhí)行命令就會對應(yīng)生成兩個py文件

hello_pb2.py

hello_pb2_grpc.py

python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto

4、py服務(wù)端代碼hello.server.py:

from concurrent import futures
import time
import grpc
import hello_pb2
import hello_pb2_grpc
import json
# 實現(xiàn) proto 文件中定義的 GreeterServicer
class Greeter(hello_pb2_grpc.GreeterServicer):
 # 實現(xiàn) proto 文件中定義的 rpc 調(diào)用
 def SayHello(self, request, context):
 return hello_pb2.HelloReply(message = 'hello {msg}'.format(msg = request.name))
 def SayHelloAgain(self, request, context):
 return hello_pb2.HelloReply(message='hello {msg}'.format(msg = request.name))

class Gretera(hello_pb2_grpc.GreeteraServicer):
 def SayStudent(self,request,context):
 print(request.id)
 if request.id=="0":
 c=hello_pb2.Student(msg=json.dumps({"name":"owen","age":22,"sex":"男"}))
 else:
 c=hello_pb2.Student(msg=json.dumps({"name":"lihui","age":23,"sex":"女"}))
 return c
def serve():
 # 啟動 rpc 服務(wù)
 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
 hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
 hello_pb2_grpc.add_GreeteraServicer_to_server(Gretera(),server)
 server.add_insecure_port('[::]:50052')
 server.start()
 try:
 while True:
 time.sleep(60*60*24) # one day in seconds
 except KeyboardInterrupt:
 server.stop(0)
if __name__ == '__main__':
 serve()

py客戶端代碼hello.client.py:

import grpc
import hello_pb2
import hello_pb2_grpc
import json
def run():
 # 連接 rpc 服務(wù)器
 channel = grpc.insecure_channel('localhost:50051')
 # 調(diào)用 rpc 服務(wù)
 stub = hello_pb2_grpc.GreeterStub(channel)
 response = stub.SayHello(hello_pb2.HelloRequest(name='czl'))
 print("Greeter client received: " + response.message)
 response = stub.SayHelloAgain(hello_pb2.HelloRequest(name='nsdnfkjda'))
 print("Greeter client received: " + response.message)
 stub1 = hello_pb2_grpc.GreeteraStub(channel)
 response1 = stub1.SayStudent(hello_pb2.Studentid(id='1'))
 print(json.loads(response1.msg))
if __name__ == '__main__':
 run()

golang

由于grpc是跨語言的所以這里用golang做為示范,golang客戶端代碼,小編這里也踩了許多坑,最主要的是兩個proto文件一定要一致,golang 中使用必須安裝protoc,windows將環(huán)境變量指向安裝目錄的bin下面:

1、protocal buffer安裝

https://github.com/google/protobuf/releases  下載 對應(yīng)自己的系統(tǒng)(環(huán)境變量記得改)

2、安裝 golang protobuf

go get -u github.com/golang/protobuf/proto // golang protobuf 庫
go get -u github.com/golang/protobuf/protoc-gen-go //protoc --go_out 工具

3、安裝 gRPC-go

go get google.golang.org/grpc

4、生成go文件

protoc --go_out=plugins=grpc:文件目錄 對應(yīng)的.proto文件
protoc --go_out=plugins=grpc:. hello.proto

生成hello.pb.go,調(diào)用的實現(xiàn)hello_go_client.go:

package main
import (
 "context"
 "encoding/json"
 "google.golang.org/grpc"
 "log"
 "student/test" //對應(yīng)的生成文件目錄
)
type Studenmsg struct {
 Name string
 Age int
 Sex string
}
func main() {
 // 建立連接到gRPC服務(wù)
 conn, err := grpc.Dial("127.0.0.1:50052", grpc.WithInsecure())
 if err != nil {
 log.Fatalf("did not connect: %v", err)
 }
 // 函數(shù)結(jié)束時關(guān)閉連接
 defer conn.Close()
 // 創(chuàng)建Waiter服務(wù)的客戶端
 t := test.NewGreeteraClient(conn)
 tr,err:=t.SayStudent(context.Background(),&test.Studentid{Id:"1"})
 if err != nil {
 log.Fatalf("could not greet: %v", err)
 }
 var st Studenmsg
 err=json.Unmarshal([]byte(tr.Msg),&st)//這里說明一下發(fā)過來的數(shù)據(jù)是json格式轉(zhuǎn)化成struct
 if err!=nil{
 log.Println(err.Error())
 }
 log.Println(st.Name,st.Age,st.Sex)
}

總結(jié)

到此這篇關(guān)于python golang中g(shù)rpc 使用示例代碼詳解的文章就介紹到這了,更多相關(guān)python golang grpc 使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何查看Python安裝了哪些包

    如何查看Python安裝了哪些包

    這篇文章主要給大家介紹了關(guān)于如何查看Python安裝了哪些包的相關(guān)資料, Conda是另一種廣泛使用的Python包管理工具,它用于安裝、管理和升級軟件包和其依賴項,需要的朋友可以參考下
    2023-07-07
  • 如何以Winsows Service方式運行JupyterLab

    如何以Winsows Service方式運行JupyterLab

    這篇文章主要介紹了如何以Winsows Service方式運行JupyterLab的教程
    2020-08-08
  • python matplotlib繪制三維圖的示例

    python matplotlib繪制三維圖的示例

    這篇文章主要介紹了matplotlib繪制三維圖的示例,幫助大家更好的利用python matplotlib繪制圖像,感興趣的朋友可以了解下
    2020-09-09
  • 人臉檢測——基于Flask和PaddleHub

    人臉檢測——基于Flask和PaddleHub

    這篇文章詳細(xì)介紹了基于Flask和PaddleHub來進(jìn)行人臉檢測,想詳細(xì)了解的朋友可以參考閱讀
    2023-03-03
  • pandas DataFrame.shift()函數(shù)的具體使用

    pandas DataFrame.shift()函數(shù)的具體使用

    本文主要介紹了pandas DataFrame.shift()函數(shù)的使用,pandas DataFrame.shift()函數(shù)可以把數(shù)據(jù)移動指定的位數(shù),有需要了解pandas DataFrame.shift()用法的朋友可以參考一下
    2021-05-05
  • tensorflow建立一個簡單的神經(jīng)網(wǎng)絡(luò)的方法

    tensorflow建立一個簡單的神經(jīng)網(wǎng)絡(luò)的方法

    本篇文章主要介紹了tensorflow建立一個簡單的神經(jīng)網(wǎng)絡(luò)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 一個基于flask的web應(yīng)用誕生 flask和mysql相連(4)

    一個基于flask的web應(yīng)用誕生 flask和mysql相連(4)

    一個基于flask的web應(yīng)用誕生第四篇,這篇文章主要介紹了如何讓flask和mysql進(jìn)行互聯(lián),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C++和python實現(xiàn)阿姆斯特朗數(shù)字查找實例代碼

    C++和python實現(xiàn)阿姆斯特朗數(shù)字查找實例代碼

    這篇文章主要給大家介紹了關(guān)于C++和python實現(xiàn)阿姆斯特朗數(shù)字查找的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python多線程中線程數(shù)量如何控制

    Python多線程中線程數(shù)量如何控制

    本文主要介紹了Python多線程中線程數(shù)量如何控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • python實現(xiàn)畫圓功能

    python實現(xiàn)畫圓功能

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)畫圓功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論

邯郸市| 红桥区| 固安县| 龙江县| 富顺县| 岳西县| 广安市| 贵南县| 叶城县| 天气| 盐源县| 那坡县| 萨迦县| 富蕴县| 专栏| 福贡县| 安宁市| 阳江市| 阿克陶县| 南陵县| 汕头市| 许昌县| 英德市| 克拉玛依市| 太仓市| 新野县| 定安县| 尼勒克县| 丹东市| 扎鲁特旗| 饶平县| 都兰县| 新野县| 疏勒县| 南木林县| 周至县| 金沙县| 奈曼旗| 山东省| 瑞安市| 京山县|