搜尋
首頁後端開發Python教學你好開發社區!介紹PydanticRPC:構建GRPC並連接RPC服務,而無需手動編寫Protobuf文件

Hello DEV Community! Introducing PydanticRPC: Build gRPC & Connect RPC Services Without Manually Writing Protobuf Files

這是我的首屆開發帖子,介紹了pydanticrpc ,它是一個自動創建GRPC並從Pydantic Models連接RPC服務的Python庫。 沒有更多的手冊.proto文件創建! >

github -pydanticrpc


概述 Python REST API經常利用FastApi或Flask等框架。 但是,對於優化的數據傳輸或架構優先方法,GRPC或Connect RPC是令人信服的替代方案。 傳統的RPC工作流程涉及定義.proto文件,代碼生成(使用

>或

)和集成 - 一個過程通常很麻煩。 protocbuf> pydanticrpc 簡化了這一點。 使用Pydantic模型定義RPC數據結構; Pydanticrpc動態生成Protobuf定義並啟動服務器。 >

什麼是pydanticrpc?


>關鍵功能:

自動化的Protobuf生成:

直接從您的Python類和Pydantic模型創建.proto文件。
  • >>動態代碼生成:>使用
  • >生成服務器/客戶端並集成了您的Python類。
  • 廣泛的支持:與GRPC,GRPC-WEB兼容(通過Sonora),Connect RPC(通過Connecpy)和異步(Asyncio)操作,包括服務器流量方法。 grpcio-tools
  • 本質上是:“用pydantic模型定義Python類,並立即獲得RPC服務 - 不需要.proto文件!”
  • >
  • 安裝

>通過PYPI安裝:

用法:創建GRPC服務

>

>使用
pip install pydantic-rpc
創建GRPC服務器。

>同步示例

別名pydantic_rpc.Server

揭示其方法。

生成.proto文件並啟動GRPC服務器(默認情況下為LocalHost:50051)。

異步示例
# server.py
from pydantic_rpc import Server, Message

class HelloRequest(Message):
    name: str

class HelloReply(Message):
    message: str

class Greeter:
    def say_hello(self, request: HelloRequest) -> HelloReply:
        return HelloReply(message=f"Hello, {request.name}!")

if __name__ == "__main__":
    server = Server()
    server.run(Greeter())

對於異步服務器,使用Messagepydantic.BaseModel> Greeter Server().run(Greeter())

是一個coroutine,在事件循環中運行。

用法:響應流AsyncIOServer

PydanticRPC
import asyncio
from pydantic_rpc import AsyncIOServer, Message

class HelloRequest(Message):
    name: str

class HelloReply(Message):
    message: str

class Greeter:
    async def say_hello(self, request: HelloRequest) -> HelloReply:
        return HelloReply(message=f"Hello, {request.name}!")

if __name__ == "__main__":
    server = AsyncIOServer()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(server.run(Greeter()))
>支持服務器流響應(當前僅是異步GRPC)。 下面的示例使用了奧運瑣事,展示了標準方法和流媒體方法:

server.run(Greeter())


是一單元rpc;

是服務器流,從而逐漸產生結果。 pydanticrpc生成一個.proto文件,以啟動異步GRPC服務器。 >

>用法:創建連接RPC服務

pydantic_ai>在ASGI應用中與Connect RPC的連接集成:

pydantic處理驗證。 將此集成到您現有的ASGI框架(Fastapi,starlette)中。
pip install pydantic-rpc

app用法:創建GRPC-WEB服務


> 在WSGI或ASGI應用程序中使用GRPC-WEB:

共存的GRPC-WEB和REST端點。 >

結論
# server.py
from pydantic_rpc import Server, Message

class HelloRequest(Message):
    name: str

class HelloReply(Message):
    message: str

class Greeter:
    def say_hello(self, request: HelloRequest) -> HelloReply:
        return HelloReply(message=f"Hello, {request.name}!")

if __name__ == "__main__":
    server = Server()
    server.run(Greeter())

PydanticRPC

> PydanticRPC簡化了GRPC,GRPC-WEB和連接來自Pydantic模型的RPC開發,包括服務器流。 探索Pydanticrpc GitHub存儲庫以獲取更多詳細信息。 歡迎反饋!

以上是你好開發社區!介紹PydanticRPC:構建GRPC並連接RPC服務,而無需手動編寫Protobuf文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。