Python: generic RPC Handlers

816 views
Skip to first unread message

Amit Saha

unread,
Sep 14, 2017, 4:22:27 AM9/14/17
to grpc.io
Hello,

https://grpc.io/grpc/python/grpc.html#grpc.server mentions the handlers kwarg to be "An optional list of GenericRpcHandlers used for executing RPCs.".

Is there more information on this?

Thanks,
Amit.



Evan Jones

unread,
Sep 15, 2017, 10:03:18 AM9/15/17
to grpc.io
I think the only real additional information is from reading the implementation. Its fairly simple: in _server.py, the _ServerState object holds on to a list of handlers. When a request comes in, _find_method_handler is called, which calls the GenericRpcHandler.service() method for each of the handlers in order, until one of them returns a value. It then uses that method handler to handle the request.

That said: I'm not sure why you might want to mess with this, since if you are using generated protocol buffer stubs, it should handle this for you. The reason I've dug through this is we have a fairly silly unit test to make sure our application has registered for the correct RPCs after it is initialized, since we had a bug where some but not all the handlers were registered after refactoring some code.

Hope that helps,

Evan

Nathaniel Manista

unread,
Sep 17, 2017, 12:10:59 PM9/17/17
to grpc.io, Amit Saha
On Fri, Sep 15, 2017 at 7:03 AM, Evan Jones <evan....@bluecore.com> wrote:
I think the only real additional information is from reading the implementation.

While reading the implementation should be helpful, I'd like to think that our generated-from-doc-strings API documentation is complete? As long as you're reading code, I'd recommend also reading the tests as well as the implementation; many of the tests are written as users of the generic API. Oh, and since the code generated by gRPC Python Protoc Plug-In that provides a customized API to applications is also itself a user of the generic API, reading the generated _pb2_grpc.py file would likely also be helpful.

Its fairly simple: in _server.py, the _ServerState object holds on to a list of handlers. When a request comes in, _find_method_handler is called, which calls the GenericRpcHandler.service() method for each of the handlers in order, until one of them returns a value. It then uses that method handler to handle the request.

That said: I'm not sure why you might want to mess with this, since if you are using generated protocol buffer stubs, it should handle this for you. The reason I've dug through this is we have a fairly silly unit test to make sure our application has registered for the correct RPCs after it is initialized, since we had a bug where some but not all the handlers were registered after refactoring some code.

Our expectation is that working with Protocol Buffers will be the foremost use case, especially for beginners, and that the generic API will only be used by a few advanced users. That's why even though it's there there are not examples or tutorials working with it.

On Thursday, September 14, 2017 at 4:22:27 AM UTC-4, Amit Saha wrote:
https://grpc.io/grpc/python/grpc.html#grpc.server mentions the handlers kwarg to be "An optional list of GenericRpcHandlers used for executing RPCs.". 

Is there more information on this?

On the very same page did you also see the specifications for grpc.GenericRpcHandler, grpc.HandlerCallDetails, and grpc.MethodCallHandler?
-Nathaniel
Reply all
Reply to author
Forward
0 new messages