问题描述
我在Python中使用grpc,发现两个节点之间的通信不小心遇到了StatusCode.UNAVAILABLE。
我找到了一个解决方案,它说 UNAVAILABLE 是一个可重试的错误,我们应该重试:https://github.com/grpc/grpc/issues/16515。
于是我查阅了文档,发现了这个:https://github.com/grpc/proposal/blob/master/A6-client-retries.md。本文档展示了一个配置演示,如下所示。
"retryPolicy": {
"maxAttempts": 4,"initialBackoff": "0.1s","maxBackoff": "1s","backoffMultiplier": 2,"retryableStatusCodes": [
"UNAVAILABLE"
]
}
我尝试按照此问题中的两个示例进行操作,但仍然无效:Use retryPolicy with python GRPC client
这是我的代码,这里也有另一个问题。我不太明白“”的意思。 :
json_config = json.dumps(
{
"methodConfig": [
{
# "name": [{"service": "<package>.<service>"}],"retryPolicy": {
"maxAttempts": 5,"maxBackoff": "10s","retryableStatusCodes": ["UNAVAILABLE"],},}
]
}
)
options = [
('grpc.service_config',json_config)
]
taf_grpc_client = GrpcRpcclient(RpcConfig(taf_server_host,self._taf_server_port,options=options),taf_server_proto_pb2_grpc.TafServerStub)
self._taf_grpc_client_dict[taf_server_host] = taf_grpc_client
我想知道的是Python GRPC是否支持“重试”,以及它的正确用法是什么。
解决方法
您指定的服务配置是正确的。由于没有复制案例,我将您的代码应用于我们的 HelloWorld 示例:
import pandas as pd
df = pd.DataFrame({'gender' : ['female','male','female','male'],'race' : ['group B','group C','group A','group B'],'education': ['bachelor's degree',...,'associate's degree']})
group = df.groupby(['gender','race']).count()
# print(group)
'''
education
gender race
female group A 2
group B 1
male group B 1
group C 1
'''
如果您使用 env async def run() -> None:
json_config = json.dumps({
"methodConfig": [{
"name": [{
"service": "helloworld.Greeter"
}],"retryPolicy": {
"maxAttempts": 5,"initialBackoff": "0.1s","maxBackoff": "10s","backoffMultiplier": 2,"retryableStatusCodes": ["UNAVAILABLE"],},}]
})
async with grpc.aio.insecure_channel('localhost:50051',options=(('grpc.service_config',json_config),)) as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
运行它,您应该观察到多次尝试重试连接。如果还有其他问题,请向 https://github.com/grpc/grpc/issues 提交问题。