1

Can anyone share any Python server and client code that demonstrates the use of chunking in gRPC similar to https://jbrandhorst.com/post/grpc-binary-blob-stream/? Thanks.

1 Answer 1

4

Here is the gRPC Python version of Chunker. The main logic of chunking in the servicer is implemented using Python generator.

# server.py
_CHUNKER_SIZE = 4
_DATA_TO_SEND = 'Hello gRPC Python World!'

def _chunk_bytes(data, chunker_size):
    index = 0
    while index < len(data):
        yield chunker_pb2.Chunk(
            chunk=data[index:index+chunker_size]
        )
        index += chunker_size


class Chunker(chunker_pb2_grpc.ChunkerServicer):

    @staticmethod
    def Chunker(request, unused_context):
        return _chunk_bytes(
            _DATA_TO_SEND,
            _CHUNKER_SIZE)

The client-side is straightforward. It receives response and concatenates them.

with grpc.insecure_channel('localhost:50051') as channel:
    stub = chunker_pb2_grpc.ChunkerStub(channel)
    response_iterator = stub.Chunker(empty_pb2.Empty())
    received_bytes = bytes()
    for response in response_iterator:
        received_bytes += response.chunk
print('Concatenated Response:')
print(received_bytes)

Full version available in Gist: https://gist.github.com/lidizheng/825f1b255767a90fb3a5d4be54071678

Sign up to request clarification or add additional context in comments.

2 Comments

Lidi, appreciate your help. How does this code fair for 1 GB file transfers?
Any suggestion to get in on C++? I need bi directional streaming using chunk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.