How to setup timeout for grpc.insecure_channel() in Python

4,319 views
Skip to first unread message

ma...@andrey-belyaevskiy.ru

unread,
Jul 10, 2017, 10:33:35 AM7/10/17
to grpc.io, zuba...@yandex-team.ru
Hi!

I write Python client code for gRPC based server application (written in C++) and need to handle open channel errors in some way. What is right way for it?

At least, I need setup timeout for insecure_channel() method call for case I use unreachable target or target host failed. How can I do it?

Right now if I use incorrect server I can successfully create channel and need to set timeout for every gRPC call and fail on first call. But I definitely do not want to proceed with any call attempt if server is not correct. Especially, my app is some kind of database and I think it is right way to handle server connection errors in Connection() constructor (in Python DB API 2.0 terms) not in Cursor methods where I work with actual gRPC calls.

What is right policy to handle channel connection errors (including open channel errors/timeouts) in Python gRPC?

In the docs https://grpc.io/grpc/python/grpc.html?highlight=insecure_channel#grpc.insecure_channel I see options parameter but I do not understand what they are. Which parameters can I pass? Can it be channel open timeout or something like this?

Nathaniel Manista

unread,
Jul 19, 2017, 1:20:38 PM7/19/17
to ma...@andrey-belyaevskiy.ru, grpc.io, zuba...@yandex-team.ru
On Mon, Jul 10, 2017 at 7:33 AM, <ma...@andrey-belyaevskiy.ru> wrote:
I write Python client code for gRPC based server application (written in C++) and need to handle open channel errors in some way. What is right way for it?

Probably to make use of the "channel connectivity subscription" methods.

At least, I need setup timeout for insecure_channel() method call for case I use unreachable target or target host failed. How can I do it?

Right now if I use incorrect server I can successfully create channel and need to set timeout for every gRPC call and fail on first call. But I definitely do not want to proceed with any call attempt if server is not correct. Especially, my app is some kind of database and I think it is right way to handle server connection errors in Connection() constructor (in Python DB API 2.0 terms) not in Cursor methods where I work with actual gRPC calls.

What is right policy to handle channel connection errors (including open channel errors/timeouts) in Python gRPC?

In the docs https://grpc.io/grpc/python/grpc.html?highlight=insecure_channel#grpc.insecure_channel I see options parameter but I do not understand what they are. Which parameters can I pass? Can it be channel open timeout or something like this?

Take a look at the methods available on the grpc.Channel object returned to you by grpc.insecure_channel - you should see subscribe and unsubscribe among them. Those are the means by which to monitor the channel's connectivity state. You can use them directly if you wish, but I suspect you'll be happier writing code that uses grpc.channel_ready_future to create a grpc.Future that matures if and when the channel gets connected. I think your code might look something like

my_channel = grpc.insecure_channel(<arguments>)
my_channel_ready_future = grpc.channel_ready_future(my_channel)
<do some other stuff>
try:
  my_channel_ready_future.result(timeout=_MY_TIMEOUT)
except grpc.FutureTimeoutError:
  <handle channel-did-not-connect>
else:
  <make use of connected channel>

.
-Nathaniel
Reply all
Reply to author
Forward
0 new messages