欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python下grpc與protobuf的編寫(xiě)使用示例

 更新時(shí)間:2022年04月12日 09:22:51   作者:Jeff的技術(shù)棧  
這篇文章主要為大家介紹了python下grpc與protobuf的編寫(xiě)使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪

1. python下protobuf使用

1.1 安裝protobuf

pip3.6 install grpcio #安裝grpc
pip3.6 install grpcio-tools #安裝grpc tools

1.2 protobuf3定義格式

新建protobuf文件名:hello.proto

syntax = "proto3";
message HelloRequest {
  string name = 1;
}

1.3 生成proto的python文件

cd hello.proto文件路徑下
命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto
注意:只有python生成兩個(gè)文件
命令解釋:
python3.6 -m grpc_tools.protoc:實(shí)際需要使用grpc_tools.protoc這里面的命令
--python_out=. :生成的python文件放在當(dāng)前路徑下,這是給rpc用的文件
--grpc_python_out=. :生成的python文件放在當(dāng)前路徑下,這是給grpc用的文件
-I.:指import,當(dāng)前目錄下找hello.proto文件

1.4 對(duì)比一下protobuf生成的效果

res.SerializeToString() # 序列化二進(jìn)制
res2.ParseFromString(res_str) # 反序列化二進(jìn)制
import json
from python_grpc.proto import hello_pb2
def main():
    res = hello_pb2.HelloRequest()
    res.name = "jeff"
    res_str = res.SerializeToString() # 序列化二進(jìn)制
    print(res_str) # b'\n\x04jeff'
    print(len((res_str))) # 6,和json對(duì)比,josn長(zhǎng)度為:16
    res2 = hello_pb2.HelloRequest()
    res2.ParseFromString(res_str) # 反序列化二進(jìn)制
    print(res2.name) # jeff
    res_json = {
        "name":"jeff"
    }
    print(len(json.dumps(res_json))) # 16,json和proto壓縮對(duì)比,proto壓縮后:6
if __name__ == '__main__':
    main()

2.python下grpc使用

2.1編寫(xiě)hello.proto文件

syntax = "proto3";
package services;
option go_package = "./;proto";
service Greeter {
    // 定義SayHello方法
    rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
    string name = 1; //編號(hào)
}
message HelloReply {
    string message = 1; //編號(hào)
}

2.2 生成proto的python文件

cd hello.proto文件路徑下
命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto
注意:只有python生成兩個(gè)文件
命令解釋:
python3.6 -m grpc_tools.protoc:實(shí)際需要使用grpc_tools.protoc這里面的命令
--python_out=. :生成的python文件放在當(dāng)前路徑下,這是給rpc用的文件
--grpc_python_out=. :生成的python文件放在當(dāng)前路徑下,這是給grpc用的文件
-I.:指import,當(dāng)前目錄下找hello.proto文件
注意:生成的*_grpc.py文件的導(dǎo)包需要修改,否則報(bào)錯(cuò):要讓python找到hello_pb2
import hello_pb2 as hello__pb2 改為:
from python_grpc.proto import hello_pb2 as hello__pb2

2.3 編寫(xiě)server端

from concurrent import futures
import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# 業(yè)務(wù)處理
class Greeter(hello_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return hello_pb2.HelloReply(message=f"你好,{request.name}")
# 啟動(dòng)
def start():
    # 1.實(shí)例化server
    Thread = futures.ThreadPoolExecutor(max_workers=10)  ## 設(shè)置線程池,并發(fā)大小
    server = grpc.server(Thread)
    # 2.注冊(cè)邏輯到server中
    hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    # 3.啟動(dòng)server
    server.add_insecure_port("127.0.0.1:8888")
    server.start()
    server.wait_for_termination()
if __name__ == '__main__':
    start()

2.4 編寫(xiě)cilent端

import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# rpc調(diào)用
def main():
    # 這里使用with,調(diào)用完會(huì)自動(dòng)關(guān)閉。優(yōu)雅寫(xiě)法
    with grpc.insecure_channel("127.0.0.1:8888") as channel:
        stub = hello_pb2_grpc.GreeterStub(channel)
        # 調(diào)用定義的SayHello方法
        rep = stub.SayHello(
            hello_pb2.HelloRequest(name="jeff")  # 傳遞定義的HelloRequest類型參數(shù)
        )
        return rep
# 業(yè)務(wù)代碼
def start():
    rep = main()
    print(rep.message)  # 你好,jeff
if __name__ == '__main__':
    start()

以上就是python下grpc與protobuf的編寫(xiě)使用的詳細(xì)內(nèi)容,更多關(guān)于python grpc與protobuf編寫(xiě)使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • PyTorch之怎樣選擇合適的優(yōu)化器和損失函數(shù)

    PyTorch之怎樣選擇合適的優(yōu)化器和損失函數(shù)

    這篇文章主要介紹了PyTorch怎樣選擇合適的優(yōu)化器和損失函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python存儲(chǔ)List數(shù)據(jù)到文件(text/csv/excel)幾種常見(jiàn)方法

    Python存儲(chǔ)List數(shù)據(jù)到文件(text/csv/excel)幾種常見(jiàn)方法

    在數(shù)據(jù)分析中經(jīng)常需要從csv格式的文件中存取數(shù)據(jù)以及將數(shù)據(jù)寫(xiě)書(shū)到csv文件中,下面這篇文章主要給大家介紹了關(guān)于Python存儲(chǔ)List數(shù)據(jù)到文件(text/csv/excel)的幾種常見(jiàn)方法,需要的朋友可以參考下
    2024-02-02
  • Python快速實(shí)現(xiàn)簡(jiǎn)易貪吃蛇小游戲的示例代碼

    Python快速實(shí)現(xiàn)簡(jiǎn)易貪吃蛇小游戲的示例代碼

    貪吃蛇(也叫做貪食蛇)游戲是一款休閑益智類游戲,有PC和手機(jī)等多平臺(tái)版本。既簡(jiǎn)單又耐玩。本文將利用Python語(yǔ)言快速實(shí)現(xiàn)簡(jiǎn)易貪吃蛇小游戲,感興趣的可以嘗試一下
    2022-10-10
  • django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例

    django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例

    今天小編就為大家分享一篇django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Spring http服務(wù)遠(yuǎn)程調(diào)用實(shí)現(xiàn)過(guò)程解析

    Spring http服務(wù)遠(yuǎn)程調(diào)用實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了Spring http服務(wù)遠(yuǎn)程調(diào)用實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python 字符串的有關(guān)知識(shí)詳解

    Python 字符串的有關(guān)知識(shí)詳解

    這篇文章主要為大家介紹了Python的字符串,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助,希望能夠給你帶來(lái)幫助
    2021-11-11
  • 詳解Python中 sys.argv[]的用法簡(jiǎn)明解釋

    詳解Python中 sys.argv[]的用法簡(jiǎn)明解釋

    本篇文章主要介紹了詳解Python中 sys.argv[]的用法簡(jiǎn)明解釋,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 使用matplotlib的pyplot模塊繪圖的實(shí)現(xiàn)示例

    使用matplotlib的pyplot模塊繪圖的實(shí)現(xiàn)示例

    這篇文章主要介紹了使用matplotlib的pyplot模塊繪圖的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Python可視化神器pyecharts繪制雷達(dá)圖

    Python可視化神器pyecharts繪制雷達(dá)圖

    這篇文章主要介紹了Python可視化神器pyecharts繪制雷達(dá)圖,雷達(dá)圖是以從同一點(diǎn)開(kāi)始的軸上表示的三個(gè)或更多個(gè)定量變量的二維圖表的形式顯示多變量數(shù)據(jù)的圖形方法
    2022-07-07
  • python查詢MySQL將數(shù)據(jù)寫(xiě)入Excel

    python查詢MySQL將數(shù)據(jù)寫(xiě)入Excel

    這篇文章主要介紹了python如何查詢MySQL將數(shù)據(jù)寫(xiě)入Excel,幫助大家利用python高效的辦公,感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論