503从插件获取元数据失败,并出现以下错误:HTTPSConnectionPool(host ='oauth2.googleapis.com',port = 443)

时间:2019-06-17 20:29:57

标签: python google-cloud-functions google-cloud-automl

尝试在程序中多次从AutoML自定义模型运行get_prediction时出现错误。我怎样才能解决这个问题?

def get_prediction(self,tweet,full_tweet):
    content = 'walgreens sucks'
    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }}
    params = {}
    request = prediction_client.predict(name, payload, params)  
    return request

这是错误:

  

google.api_core.exceptions.ServiceUnavailable:503从插件获取元数据失败,错误:HTTPSConnectionPool(host ='oauth2.googleapis.com',port = 443):url最多重试次数:/ token(由NewConnectionError( ':无法建立新连接:[Errno 8]提供的节点名或服务名,或者未知',))

2 个答案:

答案 0 :(得分:3)

是的,我也遇到了同样的问题

对于我来说,我犯了一个愚蠢的错误,我在函数内部启动了客户端,并且该函数是从循环中调用的。这意味着它将在每次呼叫时启动客户端。这是代码:

from google.cloud import translate

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="locaions of credential"

def sample_translate_text(text):
 """Translating Text."""

 try:
     project_id="project id"
     client = translate.TranslationServiceClient()

     parent = client.location_path(project_id, "global")

     response = client.translate_text(
         parent=parent,
         contents=[text],
         mime_type="text/plain", 

         target_language_code="en-US",
     )

     for translation in response.translations:
         return translation.translated_text
 #      
 except Exception  as e:
     print(str(e))
     return None
  for i in range(0, 1000):
      sample_translate_text("আমার মাথা ব্যাথা")

输出:

most frequently I got this error 

503 Getting metadata from plugin failed with error: HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))

然后启动客户端在该功能之外,检查下面的代码:

from google.cloud import translate

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="location of credential"
project_id="project id"
client = translate.TranslationServiceClient()

parent = client.location_path(project_id, "global")
def sample_translate_text(text):
    """Translating Text."""

    try:
        response = client.translate_text(
            parent=parent,
            contents=[text],
            mime_type="text/plain", 

            target_language_code="en-US",
        )

        for translation in response.translations:
            return translation.translated_text
    #      
    except Exception  as e:
        print(str(e))
        return None
   for i in range(0, 1000):
       sample_translate_text("আমার মাথা ব্যাথা")

这个案例我成功了

答案 1 :(得分:0)

我不知道此函数所在的确切上下文,但是可能发生的情况是您非常频繁地启动客户端,例如在一个循环中(每次调用该函数时)。解决方案是使客户端成为全局客户端,或将其作为参数传递给函数。这应该起作用:

prediction_client = automl_v1beta1.PredictionServiceClient()

def get_prediction(self,tweet,full_tweet):
    content = 'walgreens sucks'
    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }}
    params = {}
    request = prediction_client.predict(name, payload, params)  
    return request