是的,我也遇到了同样的问题
就我而言,我犯了一个愚蠢的错误,我在函数内部启动客户端,并且从循环中调用该函数。这意味着它会在每次调用时启动客户端。这是代码:
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("আমার মাথা ব্যাথা")
这个案例我成功了