Creating gRPC Python Client with Intellisense

One issue with the default Python gRPC Client generation is, it doesn’t seem to support intellisense making it difficult code. But luckily we have an external tool that adds intellisense support to gRPC Client Code. Lets see how we can create intellisense supported gRPC client code in Python.

Using Default gRPC IO Tools and MyPy

This method uses the default gRPC IO Tools along with the 3rd party package - mypy-protobuf, which provides intellisense support.

To start with, create a Python Virtual Environment - There are multiple ways to create Python Virtual Environment. Here we will use pipenv. To install pipenv, use:

pip install pipenv

To create the Python Virtual Environment, use:

pipenv shell

Now, lets install the required grpc packages. grpcio-tools is the official grpc package and mypy-protobuf is the 3rd party package that enables intellisense.

pipenv install grpcio-tools
pipenv install mypy-protobuf

Let’s now generate python grpc client from the proto file:

python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. --mypy_out=. query_server.proto

Replace query_server.proto in above command with your proto file name.

Here is an example which uses the grpc client code generated by the above command:

import  grpc
import  query_server_pb2  as  queryTypes
import  query_server_pb2_grpc  as  gRPCQueryServer

serverAddress = "localhost:50051"

# Create a channel and associate the channel to your service
channel = grpc.insecure_channel(serverAddress)
queryServer = gRPCQueryServer.QueryServerStub(channel)

# From the above service, make the required method call
# To the method, choose its input message type and pass its values
uptime = queryServer.Query(queryTypes.QueryRequest(query="Uptime"))

# Close the channel

Using Better Proto

Better Proto is an alternative implementation of gRPC implementation in Python with added features and done in a more “pythonic” way.

Similar to above, use below commands for creating a Python Virtual Environment using pipenv.

pip install pipenv
pipenv shell

Install better proto using the below command. Here pip is used instead of pipenv as, pipenv is showing some errors with the below package installation.

    pip install "betterproto[compiler]"
    pip install grpcio-tools

To generate python gRPC client code using better proto, use the below command:

python -m grpc_tools.protoc -I . --python_betterproto_out=. query_server.proto 

Replace query_server.proto in above command with your proto file name.
Here is a Python gRPC client code using better protobuf:

import asyncio
import queryserver
from grpclib.client import Channel

async def main():
    # Create Channel and associate service to the channel
    channel = Channel(host="", port=50051)
    qs = queryserver.QueryServerStub(channel)

    # From above service, choose the required method and its arguments
    response = await qs.query(query="Uptime")

    # Close the channel

if __name__ == "__main__":
    # Run Main Loop

Do you have any questions or comments? Let us know in the comments.

No comments:

Feel free to leave a piece of your mind.

Powered by Blogger.