Pure-Python gRPC implementation for asyncio
Go to file
Vladimir Magamedov 95ca81b4f5 Added more type annotations, moved protocols into dedicated private module 2019-05-31 14:58:46 +03:00
docs Updated changelog, issued 0.3.0rc3 release candidate 2019-05-23 16:13:41 +03:00
examples Added example of using ProcessPoolExecutor for CPU-intensive tasks 2019-05-22 21:31:56 +03:00
grpclib Added more type annotations, moved protocols into dedicated private module 2019-05-31 14:58:46 +03:00
scripts Added aiohttp+uvloop option to the bench.py script, number of requests now is configurable 2019-05-02 20:51:18 +03:00
tests Fixed protocol.Stream.send_data method to properly wait for a positive window size, refs #74 2019-05-23 15:53:19 +03:00
.gitignore Added *.pyi files into .gitignore 2019-05-17 19:03:54 +03:00
.readthedocs.yml Fixed .readthedocs.yml file 2018-04-04 20:32:45 +03:00
.travis.yml Dropped Python 3.5 support and removed compatibility code, updated changelog 2019-03-31 18:02:38 +03:00
LICENSE.txt Added sequence diagram to show how gRPC protocol in grpclib works 2018-03-31 21:51:45 +03:00
Makefile Added example of using ProcessPoolExecutor for CPU-intensive tasks 2019-05-22 21:31:56 +03:00
README.rst Made loop argument optional in user-facing apis, updated Channel.__init__ docstring 2019-05-07 21:14:45 +03:00
pi.yaml Updated test requirements, added test37 and test38 commands 2019-05-21 18:01:05 +03:00
requirements.in Added coverage report 2019-04-28 16:35:51 +03:00
requirements.txt Updated test requirements, added test37 and test38 commands 2019-05-21 18:01:05 +03:00
setup.cfg Added mypy env into tox.ini 2019-05-21 18:08:28 +03:00
setup.py Dropped Python 3.5 support and removed compatibility code, updated changelog 2019-03-31 18:02:38 +03:00
tox.ini Fixed mypy run on CI 2019-05-21 18:39:51 +03:00

README.rst

Pure-Python gRPC implementation for asyncio
===========================================

|project|_ |documentation|_ |version|_ |tag|_ |license|_

This project is based on `hyper-h2`_ and **requires Python >= 3.6**.

.. contents::
  :local:

Example
~~~~~~~

See `examples`_ directory in the project's repository for all available
examples.

Client
------

.. code-block:: python3

  import asyncio

  from grpclib.client import Channel

  # generated by protoc
  from .helloworld_pb2 import HelloRequest, HelloReply
  from .helloworld_grpc import GreeterStub


  async def main():
      channel = Channel('127.0.0.1', 50051)
      greeter = GreeterStub(channel)

      reply: HelloReply = await greeter.SayHello(HelloRequest(name='Dr. Strange'))
      print(reply.message)

      channel.close()


  if __name__ == '__main__':
      asyncio.run(main())

Server
------

.. code-block:: python3

  import asyncio

  from grpclib.utils import graceful_exit
  from grpclib.server import Server

  # generated by protoc
  from .helloworld_pb2 import HelloRequest, HelloReply
  from .helloworld_grpc import GreeterBase


  class Greeter(GreeterBase):

      async def SayHello(self, stream):
          request: HelloRequest = 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()])
      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
~~~~~~~~~~~~

.. code-block:: shell

  $ pip3 install grpclib protobuf

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

.. code-block:: shell

  $ pip3 install --upgrade --pre grpclib

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

.. code-block:: shell

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


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

.. code-block:: shell

  $ 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:

.. code-block:: shell

  $ python3 -m grpc_tools.protoc -I. --python_out=. --python_grpc_out=. helloworld/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 ``pip/setuptools`` into your ``$PATH`` during installation of the
``grpclib`` library.

Contributing
~~~~~~~~~~~~

Use Tox_ in order to test and lint your changes.

.. _gRPC: http://www.grpc.io
.. _hyper-h2: https://github.com/python-hyper/hyper-h2
.. _grpcio: https://pypi.org/project/grpcio/
.. _Tox: https://tox.readthedocs.io/
.. _examples: https://github.com/vmagamedov/grpclib/tree/master/examples
.. |version| image:: https://img.shields.io/pypi/v/grpclib.svg?label=stable&color=green
.. _version: https://pypi.org/project/grpclib/
.. |license| image:: https://img.shields.io/pypi/l/grpclib.svg
.. _license: https://github.com/vmagamedov/grpclib/blob/master/LICENSE.txt
.. |tag| image:: https://img.shields.io/github/tag/vmagamedov/grpclib.svg?label=latest
.. _tag: https://pypi.org/project/grpclib/#history
.. |project| image:: https://img.shields.io/badge/vmagamedov%2Fgrpclib-blueviolet.svg?logo=github
.. _project: https://github.com/vmagamedov/grpclib
.. |documentation| image:: https://img.shields.io/badge/docs-grpclib.rtfd.io-blue.svg
.. _documentation: https://grpclib.readthedocs.io/en/latest/