https://github.com/klen/muffin-grpc
GRPC Support for Muffin Framework
https://github.com/klen/muffin-grpc
asyncio grpc grpc-python muffin
Last synced: 7 months ago
JSON representation
GRPC Support for Muffin Framework
- Host: GitHub
- URL: https://github.com/klen/muffin-grpc
- Owner: klen
- Created: 2021-03-04T13:17:31.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2025-07-15T11:59:05.000Z (7 months ago)
- Last Synced: 2025-07-16T00:22:35.474Z (7 months ago)
- Topics: asyncio, grpc, grpc-python, muffin
- Language: Python
- Homepage:
- Size: 253 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Contributing: .github/contributing.md
- Code of conduct: .github/code_of_conduct.md
- Codeowners: .github/codeowners
- Security: .github/security.md
Awesome Lists containing this project
README
Muffin-GRPC
############
.. image:: https://github.com/klen/muffin-grpc/workflows/tests/badge.svg
:target: https://github.com/klen/muffin-grpc/actions
:alt: Tests Status
.. image:: https://img.shields.io/pypi/v/muffin-grpc
:target: https://pypi.org/project/muffin-grpc/
:alt: PyPI Version
**Muffin-GRPC** is a plugin for the Muffin_ framework that brings gRPC support to your application.
.. contents::
Features
========
- ๐ฆ Automatically compiles `.proto` files to Python
- โ๏ธ Simplified gRPC server and client integration
- ๐ CLI commands to manage proto compilation and server lifecycle
- ๐งฉ Automatically handles proto dependencies and import fixes
- ๐งช Designed with asyncio and modern Python standards
Requirements
============
- Python >= 3.10
- `grpcio`
- `grpcio-tools`
- `protobuf`
- `muffin`
.. note:: This plugin supports only the asyncio event loop (Trio is not supported).
Installation
============
Install via pip:
.. code-block:: shell
pip install muffin-grpc
Usage
=====
Set up the plugin and attach it to your Muffin application:
.. code-block:: python
from muffin import Application
from muffin_grpc import Plugin as GRPC
app = Application("example")
grpc = GRPC(default_channel="localhost:50051")
grpc.setup(app)
Create a `helloworld.proto`:
.. code-block:: proto
syntax = "proto3";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Register the file:
.. code-block:: python
grpc.add_proto("project_name/proto/helloworld.proto")
Compile proto files:
.. code-block:: shell
muffin project_name grpc_build
This generates:
- `helloworld_pb2.py` โ messages
- `helloworld_pb2_grpc.py` โ gRPC services
- `helloworld.py` โ bundled import helper
- `__init__.py` โ so the folder is importable
.. note:: Muffin-GRPC automatically fixes Python imports.
Now implement the Greeter service:
.. code-block:: python
from .proto.helloworld import GreeterServicer, HelloReply, HelloRequest
import grpc.aio as grpc_aio
@grpc.add_to_server
class Greeter(GreeterServicer):
async def SayHello(
self, request: HelloRequest, context: grpc_aio.ServicerContext
) -> HelloReply:
return HelloReply(message=f"Hello, {request.name}!")
Run the gRPC server:
.. code-block:: shell
muffin project_name grpc_server
Client example:
.. code-block:: python
from .proto.helloworld import GreeterStub, HelloRequest
from aiohttp.web import Application, Response
@app.route("/")
async def index(request):
name = request.url.query.get("name", "anonymous")
try:
async with grpc.get_channel() as channel:
stub = GreeterStub(channel)
response = await stub.SayHello(HelloRequest(name=name), timeout=10)
return Response(text=response.message)
except grpc_aio.AioRpcError as exc:
return Response(text=exc.details())
Configuration
=============
You can configure the plugin either via `setup()` or using `GRPC_` prefixed settings in the Muffin app config.
**Available options:**
=========================== ================================ =========================================
Name Default value Description
=========================== ================================ =========================================
**build_dir** `None` Directory to store compiled files
**server_listen** `"[::]:50051"` gRPC server address
**ssl_server** `False` Enable SSL for server
**ssl_server_params** `None` Tuple of credentials for SSL server
**ssl_client** `False` Enable SSL for client
**ssl_client_params** `None` Tuple of credentials for SSL client
**default_channel** `"localhost:50051"` Default gRPC client target
**default_channel_options** `{}` Additional gRPC options
=========================== ================================ =========================================
Via `setup()`:
.. code-block:: python
grpc.setup(app, server_listen="localhost:40000")
Or from config:
.. code-block:: python
GRPC_SERVER_LISTEN = "localhost:40000"
CLI Commands
============
Build registered proto files:
.. code-block:: shell
muffin project_name grpc_build
Start the gRPC server:
.. code-block:: shell
muffin project_name grpc_server
Bug Tracker
===========
Found a bug or have a suggestion?
Submit an issue here: https://github.com/klen/muffin-grpc/issues
Contributing
============
Want to contribute? Pull requests are welcome!
Development happens at: https://github.com/klen/muffin-grpc
License
=======
Licensed under the `MIT license`_.
.. _Muffin: https://github.com/klen/muffin
.. _MIT license: http://opensource.org/licenses/MIT