How can I enable client side LB in Python ?

1,489 views
Skip to first unread message

tom.h...@gmail.com

unread,
Jun 19, 2018, 4:08:46 AM6/19/18
to grpc.io
Hi,

I would like to enable round robin load balancing on my python client.

I setup my channel as follows:

 opts = [("grpc.lb_policy_name", "round_robin",)]
 channel = grpc.insecure_channel('localhost:50051' , opts)

However, after sending 1 message succesfully, my client blocks.  Am I creating the channel incorrectly ? Can you offer a hint ?

I updated the hello world example to repoduce this issue: 

when executed, the output is as expected until :
========  with round robin =======
Connection OK
Greeter client received: Hello you
Connection OK

I tried to reproduce the same issue in the cpp client, but that works as expected

environment:
$ uname -a
Linux instance-3 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64 
GNU/Linux

$ python --version
Python 2.7.13

$ pip freeze  (selected lines)
grpcio==1.12.0
protobuf==3.5.2.post1

 Thanks,
Tom

tom.h...@gmail.com

unread,
Jun 19, 2018, 6:28:41 AM6/19/18
to grpc.io
Hi,

in addition to the previous data - below output with 

export GRPC_VERBOSITY=debug

========  with round robin =======
Connection OK
D0619 10:22:38.353257384   17694 dns_resolver.cc:280]        Start resolving.
I0619 10:22:38.353954551   17696 subchannel.cc:608]          New connected subchannel at 0x7fe32c014de0 for subchannel 0x7fe32c012370
I0619 10:22:38.354072358   17696 subchannel.cc:608]          New connected subchannel at 0x7fe32c015140 for subchannel 0x7fe32c012dc0
Greeter client received: Hello you
Connection OK
D0619 10:22:38.354897819   17694 dns_resolver.cc:280]        Start resolving.

Srini Polavarapu

unread,
Jun 20, 2018, 1:29:01 PM6/20/18
to grpc.io
Can you enable tracing using GRPC_TRACE and collect more debug data to see if there is any issue with subchannels getting disconnected.

Tom Hostyn

unread,
Jun 21, 2018, 3:14:46 AM6/21/18
to psr...@google.com, grp...@googlegroups.com
Thanks for your reply and link, Srini.

$ env | grep 
GRPC
GRPC_TRACE=all
GRPC_VERBOSITY=DEBUG
$ python greeter_client.py &> log
^C^C^C


for this log, I updated the previous test code not to do the 10 messages with default options:
 
from __future__ import print_functionimport grpcimport helloworld_pb2import helloworld_pb2_grpcdef run(round_robin=False): if round_robin: opts = [("grpc.lb_policy_name", "round_robin",)] channel = grpc.insecure_channel('localhost:50051' , opts) else: channel = grpc.insecure_channel('localhost:50051') print ("Connection OK") stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message)if __name__ == '__main__': print("========= without options ========") for _ in range (0): run() print("======== with round robin =======") for _ in range (10): run(True)




--
You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/cN_E2ASt5LA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+u...@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/8d45df8b-eacb-4c25-99ab-04d558e469ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Srini Polavarapu

unread,
Jun 26, 2018, 9:19:17 PM6/26/18
to grpc.io
Looks like this broke in 1.12 release when channel.close() was introduced. It is not necessary to call channel.close() explicitly but looks like there is some issue cleaning up the previous channel. Can you try adding channel.close() before returning from run(). I tried and it works.

Tom Hostyn

unread,
Jun 27, 2018, 9:49:02 AM6/27/18
to psr...@google.com, grp...@googlegroups.com
Thanks a lot Psrini!
Closing the channel works both on my linux host, and on the kubernetes cluster.


Srini Polavarapu

unread,
Jun 27, 2018, 11:08:57 AM6/27/18
to grpc.io
Also, it is not necessary to create a new channel for each RPC. Create the the channel once per host and reuse it for all the RPCs. gRPC manages the channel internally and reconnects if there is any connectivity issue. 

tom.h...@gmail.com

unread,
Jun 27, 2018, 11:29:48 AM6/27/18
to grpc.io
Thanks a lot for your help!!


On Tuesday, 19 June 2018 10:08:46 UTC+2, tom.h...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages