Code Monkey home page Code Monkey logo

grpclib's Introduction

This project is a pure-Python gRPC implementation, based on hyper-h2 project, requires Python >= 3.5.

Licensed under BSD-3-Clause license. See LICENSE.txt

Motivation: grpclib is intended to implement gRPC protocol in Python once for all concurrency models. However, currently grpclib supports only asyncio library and only with async/await syntax.

Note: Python 2.7 support is not planned, but you can use official grpcio library for projects with such requirements.

Installation

$ pip3 install grpclib protobuf

For the code generation you will also need a protoc compiler, which can be installed with protobuf system package:

$ brew install protobuf  # example for macOS users
$ protoc --version
libprotoc ...

Or you can use protoc compiler from the grpcio-tools Python package:

$ pip3 install grpcio-tools
$ python3 -m grpc_tools.protoc --version
libprotoc ...

Note: grpcio and grpcio-tools packages are not required in runtime, grpcio-tools package will be used only during code generation.

Protoc plugin

In order to use this library you will have to generate special stub files using plugin provided, which can be used like this:

$ python3 -m grpc_tools.protoc -I. --python_out=. --python_grpc_out=. helloworld.proto

This command will generate helloworld_pb2.py and helloworld_grpc.py files.

Plugin, which implements --python_grpc_out option is available for protoc compiler as protoc-gen-python_grpc executable, which will be installed by setuptools into your PATH during installation of the grpclib library.

Example

See example directory for a full example of the helloworld service. example/README.rst contains instructions about how to generate helloworld_pb2.py and helloworld_grpc.py files and how to run example.

Example basically looks like this:

import asyncio

from grpclib.server import Server
from grpclib.client import Channel

from .helloworld_pb2 import HelloRequest, HelloReply
from .helloworld_grpc import GreeterBase, GreeterStub

loop = asyncio.get_event_loop()

# Server

class Greeter(GreeterBase):

    async def SayHello(self, stream):
        request = await stream.recv_message()
        message = 'Hello, {}!'.format(request.name)
        await stream.send_message(HelloReply(message=message))

server = Server([Greeter()], loop=loop)
loop.run_until_complete(server.start('127.0.0.1', 50051))

# Client

channel = Channel(loop=loop)
stub = GreeterStub(channel)

async def make_request():
    response = await stub.SayHello(HelloRequest(name='World'))
    assert response.message == 'Hello, World!'

# Test request

loop.run_until_complete(make_request())

# Shutdown

server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

Where helloworld.proto contains:

syntax = "proto3";

package helloworld;

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

message HelloRequest {
    string name = 1;
}

message HelloReply {
    string message = 1;
}

Contributing

Use Tox in order to test and lint your changes.

grpclib's People

Contributors

vmagamedov avatar miracle2k avatar claws avatar vsel avatar

Watchers

John D. Pope avatar  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.