Getting started with the Dapr Python gRPC service extension
The Dapr Python SDK provides a built in gRPC server extension module, , for creating Dapr services.
You can download and install the Dapr gRPC server extension module with:
Note
The development package will contain features and behavior that will be compatible with the pre-release version of the Dapr runtime. Make sure to uninstall any stable versions of the Python SDK extension before installing the dapr-dev package.
The InvokeMethodReqest
and InvokeMethodResponse
objects can be used to handle incoming requests.
A simple service that will listen and respond to requests will look like:
app = App()
@app.method(name='my-method')
def mymethod(request: InvokeMethodRequest) -> InvokeMethodResponse:
print(request.metadata, flush=True)
print(request.text(), flush=True)
return InvokeMethodResponse(b'INVOKE_RECEIVED', "text/plain; charset=UTF-8")
A full sample can be found here.
from dapr.ext.grpc import App, BindingRequest
app = App()
@app.binding('kafkaBinding')
def binding(request: BindingRequest):
print(request.text(), flush=True)
app.run(50051)
A full sample can be found .