如何知道GRPC服务器是否可用 [英] How to know if a GRPC server is available

查看:618
本文介绍了如何知道GRPC服务器是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果gRPC客户端在服务器之前启动,则它将退出,并出现异常.但是,我正在处理的应用程序同时启动了一堆服务器和客户端(附近),并且我必须在执行顺序方面保持稳健.如果客户端在服务器之前启动,我希望它等待服务器出现.

If a gRPC client starts before the server, it exits with an exception. The application I am working on starts a bunch of servers and clients (near) simultaneously, however, and I have to be robust with respect to execution order. If the client starts before the server, I want it to wait until the server shows up.

我修改了HelloWorld python客户端示例,如下所示:

I modified the HelloWorld python client example as follows:

done = False
while not done:
    try:
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=1)
        print("Greeter client received: " + response.message)
        done = True
    except:
        print('Waiting for server connect...')
        time.sleep(1)

所以现在,如果我在服务器之前启动客户端,则会收到正在等待服务器连接..."消息,按预期方式在终端窗口中向上滚动.然后,我启动服务器,然后服务器连接起来……最终.实际上,您好"消息出现之前大约需要十秒钟,这是令人惊讶的长.为什么会这么慢,并且有一种健壮的方法来检查服务器连接?

so now if I start the client before the server, I get the 'Waiting for server connect...' message scrolling up my terminal window as expected. Then I start the server, and it connects... eventually. In fact it takes about ten seconds before the 'Hello you' messages appears, which is surprisingly long. Why might it be so slow, and is there a robust way to check for server connection?

推荐答案

看看

Take a look at ChannelConnectivity - I am a go programmer but it should be the same in python. What I do is create while/for loop and when the "ChannelConnectivity" is set to "READY" I create the client from the connection and then continue.

这篇关于如何知道GRPC服务器是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆