<commit_before>Pure-Python gRPC implementation for asyncio
===========================================

|version| |tag| |license|

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

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

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

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.

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 (for Python>=3.7):

.. code-block:: python

    import asyncio

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

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


    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 test():
        loop = asyncio.get_event_loop()

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

        # perform request
        channel = Channel('127.0.0.1', 50051, loop=loop)
        stub = GreeterStub(channel)
        response = await stub.SayHello(HelloRequest(name='World'))
        print(response.message)
        channel.close()

        # shutdown server
        server.close()
        await server.wait_closed()


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

Where ``helloworld.proto`` contains:

.. code-block:: protobuf

    syntax = "proto3";

    package helloworld;

    message HelloRequest {
      string name = 1;
    }

    message HelloReply {
      string message = 1;
    }

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

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/
.. |version| image:: https://img.shields.io/pypi/v/grpclib.svg?label=stable&color=green
.. |license| image:: https://img.shields.io/pypi/l/grpclib.svg
.. |tag| image:: https://img.shields.io/github/tag/vmagamedov/grpclib.svg?label=latest<commit_msg>Linked GitHub project from the docs<commit_after>Pure-Python gRPC implementation for asyncio
===========================================

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

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

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

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

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.

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 (for Python>=3.7):

.. code-block:: python

    import asyncio

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

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


    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 test():
        loop = asyncio.get_event_loop()

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

        # perform request
        channel = Channel('127.0.0.1', 50051, loop=loop)
        stub = GreeterStub(channel)
        response = await stub.SayHello(HelloRequest(name='World'))
        print(response.message)
        channel.close()

        # shutdown server
        server.close()
        await server.wait_closed()


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

Where ``helloworld.proto`` contains:

.. code-block:: protobuf

    syntax = "proto3";

    package helloworld;

    message HelloRequest {
      string name = 1;
    }

    message HelloReply {
      string message = 1;
    }

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

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/
.. |version| image:: https://img.shields.io/pypi/v/grpclib.svg?label=stable&color=green
.. |license| image:: https://img.shields.io/pypi/l/grpclib.svg
.. |tag| image:: https://img.shields.io/github/tag/vmagamedov/grpclib.svg?label=latest
.. |project| image:: https://img.shields.io/badge/vmagamedov%2Fgrpclib-blueviolet.svg?logo=github
.. _project: https://github.com/vmagamedov/grpclib
{'fraction_non_alphanumeric': 0.1388752455795678, 'fraction_numerical': 0.01068271119842829, 'mean_word_length': 3.40032414910859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 14, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4243910', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2470, 'n_words': 748}