I have issue with asynchronous server streaming. I have written python code 
to send file from server based on the client request. The client request is 
being sent to server but the file sent in bytes from server is not being 
received. The file is created but empty content in it.

Below are the server and client code 
*1. Server*

*import os*

*import asyncio*
*import logging*
*import grpc*
*import time*
*import chunk_pb2, chunk_pb2_grpc*
*CHUNK_SIZE = 1024 * 1024 * 9  *

*async def get_file_chunks(filename):*
*    with open(filename, 'rb') as f:*
*        while True:*
*            piece = f.read(CHUNK_SIZE)*
*            if len(piece) == 0:*
*                return*
*            yield chunk_pb2.Chunk(piece)*

*class Servicer(chunk_pb2_grpc.FileServerServicer):*
*    def __init__(self):*
*        self.tmp_file_name = './tmp/server_tmp.csv'*

*     async def download(self, request, context):*
*        print(request.name)*
*        if request.name:*
*            return get_file_chunks(self.tmp_file_name)*


*async def serve(port) -> None:*
*    maxMsgLength = 10 * 1024 * 1024*
*    options=[('grpc.max_message_length', maxMsgLength),*
*                                        ('grpc.max_send_message_length', 
maxMsgLength),*
*                                        ('grpc.max_receive_message_length', 
maxMsgLength),*
*                                        ('grpc.enable_http_proxy', 0)]*
*    server = grpc.aio.server(options=options)*
*    chunk_pb2_grpc.add_FileServerServicer_to_server(Servicer(), server)*
*    server.add_insecure_port(f'[::]:{port}')*
*    logging.info("Starting server on "+ f'[::]:{port}')*
*    await server.start()*
*    try:*
*        await server.wait_for_termination()*
*    except KeyboardInterrupt:*
*        await server.stop(0)*


*if __name__ == '__main__':*
*    logging.basicConfig(level=logging.DEBUG)*
*    try:*
*        asyncio.run(serve(50051))*
*    except:*
*        print("exited")*

*2. Client*


*import os*

*import grpc*
*import time*
*import logging*
*import asyncio*
*import chunk_pb2, chunk_pb2_grpc*
*CHUNK_SIZE = 1024 * 1024 *9  *

*async def save_chunks_to_file(chunks, filename):*
*    with open(filename, 'wb') as f:*
*        async for chunk in chunks:*
*            f.write(chunk.buffer)*
        


*class FileClient:*
*    def __init__(self, address):*
*        maxMsgLength = 10 * 1024 * 1024*
*        options=[('grpc.max_message_length', maxMsgLength),*
*                                         ('grpc.max_send_message_length', 
maxMsgLength),*
*                                         ('grpc.max_receive_message_length', 
maxMsgLength),*
*                                         ('grpc.enable_http_proxy', 0)]*
*        channel = grpc.aio.insecure_channel(address, options=options)*
*        self.stub = chunk_pb2_grpc.FileServerStub(channel)*

*    async def download(self, target_name, out_file_name):*
*        response = self.stub.download(chunk_pb2.Request(name=target_name))*
*        await save_chunks_to_file(response, out_file_name)*


*async def run():*
*    client = FileClient('localhost:50051')*

*    out_file_name = './tmp/abc.csv'*
*    if os.path.exists(out_file_name):*
*        os.remove(out_file_name)*
*    await client.download('whatever_name', out_file_name)*

*if __name__ == '__main__':*
*    logging.basicConfig()*
*    asyncio.run(run())*

*3. proto *


*syntax = "proto3";*

*service FileServer {*
*  rpc download(Request) returns (stream Chunk) {}*
*}*

*message Chunk {*
*  bytes buffer = 1;*
*}*

*message Request {*
*  string name = 1;*
*}*

Kindly help me out with this


-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/47d92cc1-5cf8-4b16-9f91-eb355ee27445n%40googlegroups.com.

Reply via email to