Giter VIP home page Giter VIP logo

py-grpc's Introduction

gRPC with Python

Small example on how to setup an gRPC (remote procedure call) server and interact with it via an client.
Lets you write items to an in-memory Database and gives you a psersonalized greeting :)

In contrast to a "traditional" REST Api, every RPC method in the gRPC service is accessible via a unique endpoint.
These endpoints are called by the clients using gRPC stubs.

Files overview

The file greeter_pb2 contains message classes for your Protocol Buffers message types.
The file greeter_pb2_grpc contains gRPC client and server classes for your service definitions.
Both are generated automatically from the compiling of the protobuf file greeter.proto.

Some terms in the world of gRPC

In gRPC, a "stub" is a client-side representation of the remote service. It acts as a local proxy for the remote methods provided by the gRPC server.

When you define gRPC services and messages using Protocol Buffers, you compile them using the protoc compiler, which generates client and server code in your chosen language. The generated client code includes a stub class, which provides methods corresponding to the RPCs defined in your service.

Here's what the stub does:

Method Invocation: The stub provides methods that correspond to the RPCs defined in your service. For example, if your service defines a method called AddItem, the stub will provide a method with the same name on the client side.

Serialization and Deserialization: When you invoke a method on the stub, it handles serializing the request message into bytes (marshalling) and sending it over the network to the gRPC server. It also handles deserializing the response bytes back into the corresponding message object (unmarshalling).

Network Communication: The stub abstracts away the network communication details. You don't need to worry about low-level networking code; you simply call methods on the stub, and it handles the communication with the remote server.

Asynchronous and Synchronous Calls: Depending on your language and preferences, the stub may provide both synchronous and asynchronous versions of the RPC methods. Synchronous calls block until a response is received, while asynchronous calls return a future or a callback, allowing you to continue other tasks while waiting for the response.

In summary, the stub is a crucial component in gRPC client-side programming. It provides a convenient and high-level interface for interacting with remote gRPC services.

How to run

We first need to compile the protobuf file

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

Start the server

python server.py 

In another terminal:
Send requests

python client.py

Run with Docker:

docker build -t grpcpy .
docker run -dp 50051:50051 grpcpy

NOTE: I included 2 Dockerfiles, the normal on is pretty straightforward with a nonroot-user using the python slim base image.
The second one is the most minimal, using a distroless base-image and build from a venv.
Its smaller than ~100MB which is pretty good for python ;)

py-grpc's People

Contributors

xamma avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.