如何避免来自 Google Vision API 的 Http 错误代码 429? [英] How can I avoid Http Error Code 429 from Google Vision API?

查看:95
本文介绍了如何避免来自 Google Vision API 的 Http 错误代码 429?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Google Vision API 在一些使用 Python 的文档中执行 OCR 任务.

I've been using Google Vision API to perform OCR tasks in some documents using Python.

它开始完美运行,直到我开始收到 Http 错误代码 429,这意味着我在短时间内执行了太多请求.然后,我决定在每个请求之间进行一次睡眠,其中时间随着 Http Error Code 429 数量的增加而增加.但是,一段时间后,错误消息不断出现.由于消息不断到达,睡眠时间不断增加,直到达到睡眠时间太长以至于我失去连接的程度.

It begins working perfectly, until I start receiving Http Error Code 429, which means I am doing too many requests in a short amount of time. Then, I decided to put a sleep between each request, of which time increases as the number of Http Error Code 429 increases. However, after some time, the error message keeps coming. Since the messages keeps arriving, the sleeping time keeps increasing until it reaches a point that it sleeps for so long that I lose connection.

最奇怪的是,如果我连续多次收到这样的错误消息,并立即完成该过程并再次启动它,请求会在第一次尝试时再次开始工作.

The weirdest thing is that if I receive such error message many times in a row and, immediately, finish the process and start it again, the requests start to work again in the first try.

换句话说,似乎无论我放置多少睡眠时间,我都会在某个时候开始接收此类消息,而让它再次工作的唯一方法是重新启动该过程(这完全没有意义).

In other words, it seems that no matter the sleeping time I put I will start receiving such messages at some point and the only way to put it work again is restarting the process (which makes no sens at all).

如何在无需重新启动进程的情况下避免出现此类错误消息?有人可以帮我吗?

How can I avoid having such error message without having to restart the process? Can anyone help me?

非常感谢!

这是请求的代码(部分).

This is the code of the request (part of it).

    from apiclient import discovery
    from oauth2client.client import GoogleCredentials
    # The other imports are omitted

    DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'  # noqa
    credentials = GoogleCredentials.get_application_default()
    self.vision = discovery.build(
        'vision', 'v1', credentials=credentials,
        discoveryServiceUrl=DISCOVERY_URL)

    batch_request = []

    for image in images:
        batch_request.append({
            'image': {
                'content': base64.b64encode(image).decode('UTF-8')
            },
            'features': [{
                'type': 'TEXT_DETECTION',
            }]
        })

    request = self.vision.images().annotate(
        body={'requests': batch_request})

推荐答案

您已使用,应用程序默认凭据,

You have used, application default credentials,

credentials = GoogleCredentials.get_application_default()

也许它无法找到凭据并用于请求,因此向 API 发出匿名请求,在您的情况下不允许超过 2 或 3 个请求,我也遇到了同样的问题并找到了工作周围为:

Maybe it is not able to find the credentials and use for request and so making anonymous request to API, that is not allowing more than 2 or 3 requests as in your case, I was also facing the same issue and found the work around as:

(注意:如果您尚未设置 API 密钥或服务帐户密钥,请参阅此 doc 创建一个.)

(Note: If you haven't set up an API key or service account key, please refer this doc to create one.)

用于开发:使用 API 密钥

for development: using API key

你可以像这样使用它:

self.vision = discovery.build(
    'vision', 'v1', credentials=credentials,
    discoveryServiceUrl=DISCOVERY_URL, developerKey='your_api_key'

)

用于生产:使用服务帐号密钥

for production: using service account key

from oauth2client.service_account import ServiceAccountCredentials
scopes = ['https://www.googleapis.com/auth/sqlservice.admin']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    '/path/to/keyfile.json', scopes=scopes)

您可以在此处找到要使用的范围列表.

you can find a list of scopes to use here.

另外,你需要设置这个环境变量:

Also, you need to set this environment variable:

GOOGLE_APPLICATION_CREDENTIALS="/path/to/secret-key-file"

您不需要为每个请求增加睡眠时间,只需在使用上述任何一种方法时请求失败时增加睡眠时间.在文档中寻找指数退避算法.

You need not increase the sleep time with every request, just increase it if a request fails while using any of the above-mentioned approaches. look for the exponential backoff algorithm in docs.

这篇关于如何避免来自 Google Vision API 的 Http 错误代码 429?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆