grpclib

Pure-Python gRPC implementation for asyncio

BSD-3-Clause 57 个版本 Python >=3.10
Volodymyr Magamedov <vladimir@magamedov.com>
安装
pip install grpclib
poetry add grpclib
pipenv install grpclib
conda install grpclib
描述

Pure-Python gRPC implementation for asyncio

.. image:: https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/7e1631d13476f1e870af0d5605b643fc14471a6d/banner-direct-single.svg :target: https://savelife.in.ua/en/

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

This project is based on hyper-h2_ and requires Python >= 3.10.

.. 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
版本列表
0.4.9 2025-12-14
0.4.8 2025-05-04
0.4.7 2023-12-24
0.4.6 2023-10-02
0.4.5 2023-06-22
0.4.4 2023-05-11
0.4.3 2022-08-12
0.4.2 2021-08-25
0.4.1 2020-09-21
0.4.0 2020-08-28
0.4.9rc1 2025-12-14
0.4.8rc2 2024-07-24
0.4.8rc1 2024-04-18
0.4.7rc1 2023-10-28
0.4.3rc3 2022-06-03
0.4.3rc2 2022-02-02
0.4.3rc1 2021-11-29
0.4.2rc2 2021-08-20
0.4.2rc1 2021-08-20
0.4.0rc1 2020-07-27
0.3.2 2020-06-05
0.3.1 2019-11-06
0.3.0 2019-08-03
0.3.3rc1 2020-06-12
0.3.2rc2 2020-05-30
0.3.2rc1 2020-03-20
0.3.1rc2 2019-09-18
0.3.1rc1 2019-08-29
0.3.0rc5 2019-07-10
0.3.0rc4 2019-06-02
0.3.0rc3 2019-05-23
0.3.0rc2 2019-05-14
0.3.0rc1 2019-03-31
0.2.5 2019-05-23
0.2.4 2019-05-03
0.2.3 2019-03-31
0.2.2 2019-03-01
0.2.1 2018-12-15
0.2.0 2018-10-16
0.2.3rc3 2019-03-29
0.2.3rc2 2019-03-27
0.2.3rc1 2019-03-12
0.2.2rc5 2019-02-25
0.2.2rc4 2019-02-14
0.2.2rc3 2019-02-11
0.2.2rc2 2019-01-28
0.2.2rc1 2019-01-25
0.2.1rc1 2018-10-30
0.2.0rc2 2018-10-02
0.2.0rc1 2018-08-27
0.1.1 2018-07-03
0.1.0 2018-03-30
0.1.1rc3 2018-06-23
0.1.1rc2 2018-06-08
0.1.1rc1 2018-05-29
0.1.0rc2 2017-12-13
0.1.0rc1 2017-11-30