【问题标题】:grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLEgrpc._channel._InactiveRpcError: <_InactiveRpcError of RPC 终止于:status = StatusCode.UNAVAILABLE
【发布时间】:2021-05-19 07:28:27
【问题描述】:

我用python写了一个简单的GRPC服务,客户端代码。有时,客户端会突然出现以下错误:

Traceback (most recent call last):
  File "grpc_client.py", line 35, in <module>
    response = stub.YOLO_frame(image_req)
  File "/home/vmuser/anaconda3/envs/lp_reg_brta/lib/python3.7/site-packages/grpc/_channel.py", line 923, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/vmuser/anaconda3/envs/lp_reg_brta/lib/python3.7/site-packages/grpc/_channel.py", line 826, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses"
        debug_error_string = "{"created":"@1613478605.328638006","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":5390,"referenced_errors":[{"created":"@1613478605.328628806","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":397,"grpc_status":14}]}"
>

我的服务器:

import grpc
from concurrent import futures
import time

import darknet

# import the generated classes
import lp_reg_pb2
import lp_reg_pb2_grpc



# based on .proto service
class LP_REGServicer(lp_reg_pb2_grpc.LP_REGServicer):
    def YOLO_frame(self, request, context):
        response = lp_reg_pb2.PredictionBBox()
        # print(type(request.b64_frame))


        response.class_labels.extend([1])
        response.xc.extend([1])
        response.yc.extend([1])
        response.w.extend([1])
        response.h.extend([1])
        
        # darknet.dummy_predict_grpc(request.b64_frame)
        return response

    def YOLO_lp(self, request, context):
        response = lp_reg_pb2.LPBBox()
        response.class_labels.extend([1])
        response.xc.extend([1])
        response.yc.extend([1])
        response.w.extend([1])
        response.h.extend([1])
        return response

# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=12))


# add the defined class to the server
lp_reg_pb2_grpc.add_LP_REGServicer_to_server(
        LP_REGServicer(), server)

# listen on port 5005
print('Starting server. Listening on port 5005.')
server.add_insecure_port('[::]:5005')
server.start()

try:
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    server.stop(0)

我的客户代码:

import grpc

# import the generated classes
import lp_reg_pb2
import lp_reg_pb2_grpc

# data encoding

import numpy as np 
import base64
import zlib
import time
import cv2

# open a gRPC channel
channel = grpc.insecure_channel('127.0.0.1:5005')

# create a stub (client)
stub = lp_reg_pb2_grpc.LP_REGStub(channel)

# encoding image/numpy array

t1 = time.time()
for _ in range(10):
    frame = cv2.resize( cv2.imread('prediction.jpg'), (416, 416) ) # dummy rgb image

    img_encoded = cv2.imencode('.jpg', frame)[1]

    img_bytes = img_encoded.tobytes()  # bytes class

    # create a valid request message
    image_req = lp_reg_pb2.B64Frame(b64_frame = img_bytes)

    # make the call
    response = stub.YOLO_frame(image_req)
    print(response)
t2 = time.time()

print(f"time: {(t2-t1)/10.}")

我尝试在客户端代码中添加channel.close(),但它仍然抛出此错误而没有任何正确响应。

  • 我更改端口后客户端可以正常工作,但一段时间后,我再次收到此错误。

【问题讨论】:

  • 您的服务器正在侦听 IPv6 任意地址 [::],而客户端正尝试与 IPv4 地址 (127.0.0.1) 通信。这是故意的吗?这到底是怎么回事?
  • 这是文档告诉我的grpc.io/docs/languages/python/basics
  • 嘿@ZabirAlNazi 我遇到了同样的错误,你是怎么解决的?
  • 我的问题是在服务器端,而不是在客户端
  • @ZabirAlNazi 谢谢,我已经解决了这个问题,但我不记得我是如何解决的了。

标签: python-3.x grpc grpc-python


【解决方案1】:

我从https://github.com/grpc/grpc/issues/9987找到了解决方案

unset http_proxy; unset https_proxy; python server.py &
unset http_proxy; unset https_proxy; python client.py

我还发现额外的options 可以传递给grpc.insecure_channel

在客户端代码中,

channel = grpc.insecure_channel('localhost:5005', options=(('grpc.enable_http_proxy', 0),))

这解决了问题。

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2023-03-17
    • 2022-01-07
    • 2017-03-23
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多