【问题标题】:gRPC failed to connect to all addresses - PythongRPC 无法连接到所有地址 - Python
【发布时间】:2021-12-06 12:48:11
【问题描述】:

我正在尝试连接到 Clarifai 的“通用”图像分类模型,以使用以下 Python 脚本标记图像:

#python program to analyze an image and label it
##############################################################################
# Installation
##############################################################################

#pip install clarifai-grpc

##############################################################################
## Initialize client
##     - This initializes the gRPC based client to communicate with the 
##       Clarifai platform. 
##############################################################################
## Import in the Clarifai gRPC based objects needed
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2

## Construct the communications channel and the object stub to call requests on.
# Note: You can also use a secure (encrypted) ClarifaiChannel.get_grpc_channel() however
# it is currently not possible to use it with the latest gRPC version
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)


################################################################################
## Set up Personal Access Token and Access information
##     - This will be used by every Clarifai API call 
################################################################################
## Specify the Authorization key.  This should be changed to your Personal Access Token.
## Example: metadata = (('authorization', 'Key 123457612345678'),) 
metadata = (('authorization', 'Key <PERSONAL ACCESS TOKEN>'),)

##
## A UserAppIDSet object is needed for most rpc calls.  This object contains
## two pieces of information: the user id and the app id.  Both of these are
## specified as string values.
##
##     'user_id' : This is your user id
##     'app_id'  : This is the app id which contains the model of interest
userDataObject = resources_pb2.UserAppIDSet(user_id='<USERNAME>', app_id='<APP ID>')

# Insert here the initialization code as outlined on this page:
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

post_model_outputs_response = stub.PostModelOutputs(
    service_pb2.PostModelOutputsRequest(
        user_app_id=userDataObject,  # The userDataObject is created in the overview and is required when using a PAT
        model_id="aaa03c23b3724a16a56b629203edc62c",
        version_id="aa7f35c01e0642fda5cf400f543e7c40",  # This is optional. Defaults to the latest model version.
        inputs=[
            resources_pb2.Input(
                data=resources_pb2.Data(
                    image=resources_pb2.Image(
                        url="https://samples.clarifai.com/metro-north.jpg"
                    )
                )
            )
        ]
    ),
    metadata=metadata
)
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
    print("There was an error with your request!")
    print("\tCode: {}".format(post_model_outputs_response.outputs[0].status.code))
    print("\tDescription: {}".format(post_model_outputs_response.outputs[0].status.description))
    print("\tDetails: {}".format(post_model_outputs_response.outputs[0].status.details))
    raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)

# Since we have one input, one output will exist here.
output = post_model_outputs_response.outputs[0]

print("Predicted concepts:")
for concept in output.data.concepts:
    print("%s %.2f" % (concept.name, concept.value))

但是,我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Eshaan\Desktop\moveForward\Python\Level 4\New try\Vision MFSA.py", line 45, in <module>
    post_model_outputs_response = stub.PostModelOutputs(
  File "C:\Users\Eshaan\AppData\Local\Programs\Python\Python39\lib\site-packages\grpc\_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\Eshaan\AppData\Local\Programs\Python\Python39\lib\site-packages\grpc\_channel.py", line 849, 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":"@1634655221.541000000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3009,"referenced_errors":[{"created":"@1634655221.541000000","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":398,"grpc_status":14}]}"
>

关于为什么会这样以及如何解决它的任何想法/建议?我参考了以下文档:https://docs.clarifai.com/api-guide/predict/images

我没有从我这里添加任何代码,它全部来自文档以及所需的密钥。我已经查看了网站上所有相关的答案,但仍然无法弄清楚。

【问题讨论】:

  • 你安装了什么版本的gRPC?当 LetsEncrypt 在 9 月更改一项我不确定是否已解决的策略时,发生了一个已知的证书问题。
  • 是的,我也读到过,顺便问一下如何查看 gRPC 的版本?
  • 您可以使用python -m pip freeze 进行检查,尽管我在您可以使用的答案中添加了一个更简单的修复程序。但是要更新 gRPC 库,您也可以执行 python -m pip install --upgrade grpcio grpcio-tools(他们可能有一个修复它的版本)

标签: python grpc-python clarifai


【解决方案1】:

gRPC Core 的新版本 1.41.1 修复了它https://github.com/grpc/grpc/pull/27539

【讨论】:

    【解决方案2】:

    目前 gRPC 在如何支持某些证书方面存在问题。您或许可以更新到最新版本以解决此问题,或者作为替代方案,您可以使用稍微不同的方法并进行更改:

    channel = ClarifaiChannel.get_grpc_channel()

    channel = ClarifaiChannel.get_json_channel()

    这基本上可以通过在幕后使用 REST 来回避问题。

    (注意:包括解析在内的程序的其余部分将继续按预期工作,即使发生了这种变化,它只是改变了通信方式。)

    【讨论】:

    • 你,是天使!
    • 谢谢 :) 如果它有帮助,请接受答案,以便其他人在搜索相同问题时知道它解决了问题!
    • 是的,我做到了.....
    • 这很奇怪,我只看到它被赞成,而不是被选为答案。
    • 我的错,我现在就这么做了。
    猜你喜欢
    • 2019-12-27
    • 2020-05-06
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-05-12
    • 2021-04-08
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多