0

I hava a python grpc service that streams large amounts of data to client microservices.

service GeoService {
   rpc GetGeoCoordinates(GetRequest) returns (stream Chunk){}
}

message Chunk {
    bytes data_part = 1;
}

I can not send more that 4MB of data at once because tcp connection has limits. Here is my code (only relevant part):

def GetGeoCoordinates(self, request, context):
    ...
    ...

    dataBytes = geo_pb2.Chunk(data_part=bytes(json.dumps(coordinates["data"]), 'utf-8'))

    yield dataBytes

How can I send this data in 4MB chunks?

Also is it a good practice to json.dumps() large data then stream? Any help is appreciated.

2
  • check if the result can be consumed as stream Commented Oct 12, 2023 at 8:44
  • @RomanPerekhrest I fixed it. Now it is being consumed. Now I need to stream it in 4MB chunks. I would really appreciate if you help me on this Commented Oct 12, 2023 at 11:56

1 Answer 1

0

After digging through SO and looking at many answers I finally managed to achieve what I expected:

 def GetGeoCoordinates(self, request, context):
    CHUNK_SIZE = 1024 * 1024 * 4     # 4 mb
    ...
    ...
    
    # convert data to bytes
    dataBytes = bytes(json.dumps(coordinates["data"]), 'utf-8')

    # stream in 4MB chunks
    index = 0
    while index < len(dataBytes):
        yield geo_pb2.Chunk(
            data_part=dataBytes[index:index+CHUNK_SIZE]
        )
        index += CHUNK_SIZE

This answer helped me the most Chunking in gRPC with Python

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

Comments

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.