Python grpc async client

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Pure-Python gRPC implementation for asyncio

License

vmagamedov/grpclib

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.rst

Pure-Python gRPC implementation for asyncio

This project is based on hyper-h2 and requires Python >= 3.7.

See examples directory in the project’s repository for all available examples.

import asyncio from grpclib.client import Channel # generated by protoc from .helloworld_pb2 import HelloRequest, HelloReply from .helloworld_grpc import GreeterStub async def main(): async with Channel('127.0.0.1', 50051) as channel: greeter = GreeterStub(channel) reply = await greeter.SayHello(HelloRequest(name='Dr. Strange')) print(reply.message) if __name__ == '__main__': asyncio.run(main())
import asyncio from grpclib.utils import graceful_exit from grpclib.server import Server # generated by protoc from .helloworld_pb2 import HelloReply from .helloworld_grpc import GreeterBase class Greeter(GreeterBase): async def SayHello(self, stream): request = await stream.recv_message() message = f'Hello, request.name>!' await stream.send_message(HelloReply(message=message)) async def main(*, host='127.0.0.1', port=50051): server = Server([Greeter()]) # Note: graceful_exit isn't supported in Windows with graceful_exit([server]): await server.start(host, port) print(f'Serving on host>:port>') await server.wait_closed() if __name__ == '__main__': asyncio.run(main())
$ pip3 install "grpclib[protobuf]"

Bug fixes and new features are frequently published via release candidates:

$ pip3 install --upgrade --pre "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.

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=. --grpclib_python_out=. helloworld/helloworld.proto ^----- note -----^

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

Plugin which implements —grpclib_python_out option should be available for the protoc compiler as the protoc-gen-grpclib_python executable which should be installed by pip into your $PATH during installation of the grpclib library.

Changed in v0.3.2: —python_grpc_out option was renamed into —grpclib_python_out .

  • Please submit an issue before working on a Pull Request
  • Do not merge/squash/rebase your development branch while you work on a Pull Request, use rebase if this is really necessary
  • You may use Tox in order to test and lint your changes, but it is Ok to rely on CI for this matter

About

Pure-Python gRPC implementation for asyncio

Источник

Pure-Python gRPC implementation for asyncio

This project is based on hyper-h2 and requires Python >= 3.7.

Example

See examples directory in the project’s repository for all available examples.

Client

import asyncio from grpclib.client import Channel # generated by protoc from .helloworld_pb2 import HelloRequest, HelloReply from .helloworld_grpc import GreeterStub async def main(): async with Channel('127.0.0.1', 50051) as channel: greeter = GreeterStub(channel) reply = await greeter.SayHello(HelloRequest(name='Dr. Strange')) print(reply.message) if __name__ == '__main__': asyncio.run(main()) 

Server

import asyncio from grpclib.utils import graceful_exit from grpclib.server import Server # generated by protoc from .helloworld_pb2 import HelloReply from .helloworld_grpc import GreeterBase class Greeter(GreeterBase): async def SayHello(self, stream): request = await stream.recv_message() message = f'Hello, request.name>!' await stream.send_message(HelloReply(message=message)) async def main(*, host='127.0.0.1', port=50051): server = Server([Greeter()]) # Note: graceful_exit isn't supported in Windows with graceful_exit([server]): await server.start(host, port) print(f'Serving on host>:port>') await server.wait_closed() if __name__ == '__main__': asyncio.run(main()) 

Installation

$ pip3 install "grpclib[protobuf]" 

Bug fixes and new features are frequently published via release candidates:

$ pip3 install --upgrade --pre "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=. --grpclib_python_out=. helloworld/helloworld.proto ^----- note -----^ 

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

Plugin which implements —grpclib_python_out option should be available for the protoc compiler as the protoc-gen-grpclib_python executable which should be installed by pip into your $PATH during installation of the grpclib library.

Changed in v0.3.2: —python_grpc_out option was renamed into —grpclib_python_out .

Contributing

  • Please submit an issue before working on a Pull Request
  • Do not merge/squash/rebase your development branch while you work on a Pull Request, use rebase if this is really necessary
  • You may use Tox in order to test and lint your changes, but it is Ok to rely on CI for this matter

© Copyright 2019, Vladimir Magamedov. Revision 6ec444e8 .

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Example using asyncio with grpc in Python

License

sandtable/python-grpc-asyncio

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Using Python asyncio with gRPC

This repository provides a simple example of using asyncio both on the server and client side for gRPC.

Server-side asyncio code is taken from Matthew Ellison’s (seglberg) gist: https://gist.github.com/seglberg/0b4487b57b4fd425c56ad72aba9971be

pip install -r requirements.txt 

Источник

Читайте также:  How to center input css
Оцените статью