grpc python library server streaming issue

389 views
Skip to first unread message

sam reddy

unread,
Oct 31, 2017, 6:09:35 PM10/31/17
to grpc.io
a simple grpc server client, client send a int and server streams int's back.

client is reading the messages one by one but server is running the generator function immediately for all responses.

server code: 


   
import test_pb2_grpc as pb_grpc
   
import test_pb2 as pb2
   
import time
   
import grpc
   
from concurrent import futures


   
class test_servcie(pb_grpc.TestServicer):


       
def Produce(self, request, context):
           
for i in range(request.val):
               
print("request came")
                rs
= pb2.Rs()
                rs
.st = i + 1
               
yield rs






       
def serve():




            server
=
            grpc
.server(futures.ThreadPoolExecutor(max_workers=10))
            pb_grpc
.add_TestServicer_to_server(test_servcie(), server)
            server
.add_insecure_port('[::]:50051')
           
print("service started")
            server
.start()
           
try:
               
while True:
                    time
.sleep(3600)
           
except KeyboardInterrupt:
                server
.stop(0)




   
if __name__ == '__main__':
        serve
()


client code:

 
   import grpc
   
import test_pb2_grpc as pb_grpc
   
import test_pb2 as pb
   
def test():
        channel
= grpc.insecure_channel(
       
'{host}:{port}'.format(host="localhost", port=50051))


        stub
= pb_grpc.TestStub(channel=channel)


        req
= pb.Rq()
        req
.val = 20
       
for s in stub.Produce(req):
           
print(s.st)
           
import time
            time
.sleep(10)


    test
()



proto file:
    syntax
= "proto3";


    service
Test {
        rpc
Produce (Rq) returns (stream Rs);
   
}


    message
Rq{
     int32 val
= 1;
   
}
    message
Rs{
     int32 st
= 1;
   
}


after starting the server
when i run the client, server side generator started running and completed immediately it looped for the range.
what i expected is it will one by one as client calls but that is not the case.
is this an expected behaviour. my client is still printing the values but the sever is already completed the function.

Ken Payson

unread,
Oct 31, 2017, 6:13:37 PM10/31/17
to sam reddy, grpc.io
This behavior is expected for a unary request/stream response rpc.

If you want the service handler to loop over requests, you should use bidirectional streaming (stream request/ stream response).

--
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 grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/f1a4a282-f137-4ca1-91ac-b3ab3466801e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages