Pure-Python gRPC implementation for asyncio
Go to file
Vladimir Magamedov 62f968a4c8 Updated changelog, issued 0.4.8rc2 release candidate 2024-07-24 23:09:16 +03:00
.github/workflows Updated action versions in GitHub Actions workflows 2024-04-18 18:17:48 +03:00
docs Updated changelog, issued 0.4.8rc2 release candidate 2024-07-24 23:09:16 +03:00
examples Added wheel dependency, updated pinned dependencies, re-generated proto files 2024-04-18 17:40:52 +03:00
grpclib Updated changelog, issued 0.4.8rc2 release candidate 2024-07-24 23:09:16 +03:00
requirements Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
scripts Updated reference examples and benchmarks to use experimental asyncio support from grpcio 2020-06-05 23:19:44 +03:00
tests Fixed Channel to construct valid authority header when host is the IPv6 address, closes #197 2024-07-21 21:34:11 +03:00
.gitignore Updated .gitignore file 2019-06-12 17:06:39 +03:00
.readthedocs.yaml Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
LICENSE.txt Updated license information 2019-07-02 16:43:31 +03:00
Makefile Added wheel package upon release 2024-04-18 17:29:11 +03:00
README.rst Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
pi.yaml Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
pyproject.toml Using setup.cfg as a medium for the package metadata 2020-07-27 20:56:53 +03:00
setup.cfg Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
setup.py Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
setup.txt Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00
tox.ini Set Python 3.8 as minimal supported version, updated pinned dependencies 2024-04-18 18:11:30 +03:00

README.rst

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

.. image:: https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/7e1631d13476f1e870af0d5605b643fc14471a6d/banner-direct-single.svg
  :target: https://standforukraine.com

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

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

.. 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():
      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
------

.. code-block:: python3

  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
~~~~~~~~~~~~

.. code-block:: console

  $ pip3 install "grpclib[protobuf]"

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

.. code-block:: console

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

.. code-block:: console

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


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

.. code-block:: console

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

  $ 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

.. _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=blue
.. _version: https://pypi.org/project/grpclib/
.. |license| image:: https://img.shields.io/pypi/l/grpclib.svg?color=blue
.. _license: https://github.com/vmagamedov/grpclib/blob/master/LICENSE.txt
.. |tag| image:: https://img.shields.io/github/tag/vmagamedov/grpclib.svg?label=latest&color=blue
.. _tag: https://pypi.org/project/grpclib/#history
.. |project| image:: https://img.shields.io/badge/vmagamedov%2Fgrpclib-blueviolet.svg?logo=github&color=blue
.. _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/
.. |downloads| image:: https://static.pepy.tech/badge/grpclib/month
.. _downloads: https://pepy.tech/project/grpclib