melledijkstra
melledijkstra

Reputation: 359

Split up servicer gRPC python

Building my first gRPC server application. I don't know if this is a common problem, but my rpc servicer is getting quite large.

With servicer I mean the class that inherit from the generated xxx_pb2_grpc.xxxServicer (in python at least).

At the moment it has 19 RPC calls. So my class which implements these calls is getting quite large. Is there any way of splitting the Servicer into logical segments, considering that some RPC calls are for music playback, some are for data management and some for communicating with a downloader (and more to come probably).

I could make multiple service types in proto file however xxx_pb2_grpc.add_xxxServicer_to_server only accepts a single servicer!

Thanks in advance.

EDIT: FEB 3, 18

I've split up the services in the following manor:

service MusicPlayback {
    ... # rpc calls
}

service DataManager {
    ... # rpc calls
}

service MediaDownloader {
    ... # rpc calls
}

Then with the python code generated, I've setup the server like this:

# Setup gRPC Server
self._gserver = grpc.server(futures.ThreadPoolExecutor(max_workers=self._connection_count))
rpc.add_MusicPlaybackServicer_to_server(self._mplayer, self._gserver)
rpc.add_DataManagerServicer_to_server(self._data_manager, self._gserver)
rpc.add_MediaDownloaderServicer_to_server(self._media_downloader, self._gserver)
self._gserver.add_insecure_port('[::]:' + str(self._port))

Upvotes: 3

Views: 1736

Answers (1)

It is true that xxx_pb2_grpc.add_xxxServicer_to_server accepts only a single servicer. But you can create a single server, pass it to xxx_pb2_grpc.add_xxxServicer_to_server, and then also pass it (the same server) to xxx_pb2_grpc.add_xxxxxxxxxxxxxServicer_to_server, yyy_pb2_grpc.add_yyyServicer_to_server, and zzz_pb2_grpc.add_zzzServicer_to_server. It should be possible to keep services from growing to huge collections of unrelated RPC methods by taking advantage of the fact that a single server can serve arbitrarily many services (implemented with one servicer per service).

Upvotes: 5

Related Questions