Package: django-grpc-bus
Django gRPC FrameworkDjango gRPC framework is a advanced toolkit for building gRPC services.RequirementsPython (3.6, 3.7, 3.8, 3.9)Django (2.2, 3.0, 3.1)Django Rest Framework (3.10, 3.11)grpcio (1.34x)grpcio-health (1.34x)grpcio-reflection (1.34x)grpcio-tools (1.34x)isortInstallation$ pip install django-grpc-busAdddjango_grpc_bustoINSTALLED_APPSsetting:INSTALLED_APPS = [
    ...
    'django_grpc_bus',
]AddMESSAGE_BUSto settings:MESSAGE_BUS = {
    'ROOT_HANDLERS_HOOK': 'path.to.handler',
    'SERVICE_META': os.path.join(BASE_DIR, 'service_meta.yaml'),
    'PRODUCER_ROOT': os.path.join(settings.BASE_DIR, 'generated_grpc'),
    'SERVICE_TEMPLATE': os.path.join(Path(__file__).resolve().parent, 'service_template'),
    'HANDLER_TEMPLATE': os.path.join(Path(__file__).resolve().parent, 'handler_template'),
    'SERVLETS': {
        'service1': {
            'host': localhost,
            'port': 50051,
        },
        'service2': {
            'host': localhost,
            'port': 50052,
        },
        'service3': {
            'host': localhost,
            'port': 50053,
        },
    }
}ExampleLet's take a look at a quick example of using gRPC framework to serve and consume gRPC services.Defineservice_meta.ymlin the project rootapps:-name:app_1models:-name:model_1exclude:-exclude_field_1-exclude_field_2-name:app_2models:-name:model_2fields:-field_1-field_2-name:model_3By default all apps will be used as service, or to generate default service meta yaml for all apps using:./manage.py generatemetayaml --file path.to.save.fileUpdate the path ofservice_meta.yamlin MESSAGE_BUS settings.Generate default proto files (defined inservice_meta.yaml) toPRODUCER_ROOTpath, using:./manage.py generateprotoOnce the proto files are updated, generate the gRPC python out files toPRODUCER_ROOTpath, using:./manage.py generategrpcDefine the componentsserializers.py,services.pyandhandlers.pyserializers.pyare the similar to django rest serializers, example:fromdjango_grpc_busimportserializersasproto_serializersfrompath.to.generated_grpc.grpcimportModelName_pb2fromapp_nameimportmodelsclassModelNameSerializer(proto_serializers.ModelProtoSerializer):classMeta:model=models.ModelNameproto_class=ModelName_pb2.ModelNameDataservices.pycontains the gRPC producer services, inherited from class ModelService containing predefined mixinscreate,retrieve,update,destroyandlist, example to define services.py:fromdjango_grpc_bus.genericsimportModelServicefromapp_nameimportmodels,serializersclassModelNameService(ModelService):queryset=models.ModelName.objects.all()serializer_class=serializers.ModelNameSerializerhandlers.pyare the routers for services, that register the services, example:fromdjango_grpc_bus.handlersimportBasicHandlerhandler=BasicHandler()handler.register('app_name.services.ModelNameService')To start with above components simply run the following command:./manage.py generateservices -hdPredefined templates will be generated in the app directories andhandlers.pywill be get appended with service
registry.Start the server using:./manage.py rungrpcserver --devServer will get started at 50051 port.To consume the services, for now we are using gRPC reflection that lists all the registered services and its methods.
Use the client as:fromdjango_grpc_bus.client.registryimportregistrylist_data=registry.servlet_name.service_name.list()# Generator object will be returned as its stream responseget_data=registry.servlet_name.service_name.retrieve({'id':1})