我在使用 AWS 翻译时遇到 ThrottlingException api

I get ThrottlingException while using AWS translate api

我正在运行翻译这段代码

translate = boto3.client(service_name='translate',
    aws_access_key_id="secret",
    aws_secret_access_key="secret",
    region_name='eu-central-1',
    use_ssl=True)

translate.translate_text(Text=data,SourceLanguageCode="de",TargetLanguageCode="en").get("TranslatedText")

代码在大部分测试中运行正常,但突然抛出以下错误:

An error occurred (ThrottlingException) when calling the TranslateText operation (reached max retries: 4): Rate exceeded 

如何处理这个异常?

这个 link 有限制异常的答案 https://docs.aws.amazon.com/translate/latest/dg/what-is-limits.html#limits-throttling 该服务根据流量模式扩大车队规模。我想知道您达到限制的 TPS 是多少。

&

您是否也尝试过像 eu-west-1 这样的其他区域?

您是否考虑过使用 异步 调用 start_text_translation_job() 而不是 同步 调用 translate_text()?那么你会有更高的限制,而不是 5000 字节,你会有 1,000,000 个字符 * 1,000,000 个文档 * 10 个批次:https://docs.aws.amazon.com/translate/latest/dg/what-is-limits.html#limits-throttling

同步Real-Time翻译限制:

Description Limit
Character encoding  UTF-8
Maximum document size (UTF-8 characters)    5,000 bytes

异步批量翻译限制:

Description Limit
Character encoding  UTF-8
Maximum number of characters per document   1,000,000
Maximum size per document   20 MB
Maximum number of documents in batch    1,000,000
Maximum size of total documents in batch    5 GB
Maximum number of parallel batch translation jobs   10

异步start_text_translation_job()调用的代码可以在这里找到: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/translate.html

response = client.start_text_translation_job(
    JobName='string',
    InputDataConfig={
        'S3Uri': 'string',
        'ContentType': 'string'
    },
    OutputDataConfig={
        'S3Uri': 'string'
    },
    DataAccessRoleArn='string',
    SourceLanguageCode='string',
    TargetLanguageCodes=[
        'string',
    ],
    TerminologyNames=[
        'string',
    ],
    ClientToken='string'
)

这个问题已经有几个月了,但解决方案 here 对我来说效果很好,让我无需编写自己的退避代码:

import boto3
from botocore.config import Config

config = Config(retries=dict(max_attempts=10))
region = "us-east-1"

translate = boto3.client(
    service_name="translate",
    region_name=region,
    use_ssl=True,
    config=config,
)

即使在 us-east-1 中,我似乎也重试了几次,它比 Google Cloud Translate(我也在同一脚本中从 Python 中点击)慢得多,但是它有效。

运行 陷入类似的问题,不想使用 asynchronous batch requests,因为我正在使用多个 t运行slation APIs 并且宁愿保留所有人都使用相同的代码。

这里是针对不同时间 运行slation APIs 的负载限制。这可能会影响节流(例如,每秒多次调用 Amazon T运行slate API,负载较小,效果很好):

亚马逊的限额显然是最低的。

现在 Amazon T运行slate 的限制:

  • Amazon 没有说明正常 API 的节流限制是多少,文档仅说明:

    Amazon Translate scales to serve customer operational traffic. If you encounter sustained throttling, contact AWS Support.

  • 但是docs for AWS GovCloud确实提到了:

    The default throttling limits for AWS GovCloud (US) are set to 5000 Bytes per 10 seconds per language pair and 10 transactions per second per language pair. You can request an increase for any of the limits using the Amazon Translate service limits increase form.

对于我的用例(t运行slating HTML 分割成块后大于 5,000 字节),最终实现了 15 秒之间的简单等待 API 调用 以避免达到节流限制。在我的测试中,t运行slating 2K 到 5K 字节的有效负载 200 次,两次调用之间的休眠时间为 15 秒,所有 运行 都成功(而我的等待时间仅为 11 秒) d仍然得到一些ThrottlingException。)

如果我要更好地编写代码,我可能会像 Marcin 建议的那样使用指数退避 实现 重试。或者会 请求提高限额 让我的生活更轻松,让我的体验在 API 秒内更加一致。