Python 我在使用AWS translate api时遇到ThrottlingException
我正在运行此代码进行翻译Python 我在使用AWS translate api时遇到ThrottlingException,python,amazon-web-services,amazon-ec2,boto3,boto,Python,Amazon Web Services,Amazon Ec2,Boto3,Boto,我正在运行此代码进行翻译 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",TargetLa
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
如何处理此异常?此链接提供了限制异常的答案 该服务根据交通模式扩大车队规模。我想知道你达到了什么样的限制 &
您是否也尝试过其他地区,如eu-west-1?您是否考虑过使用异步调用
启动文本翻译作业()
而不是同步调用翻译文本()
?那么您将有一个更高的限制,而不是5000字节,您将有1000000个字符*1000000个文档*10个批次:
同步实时转换限制:
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
异步批转换限制:
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\u text\u translation\u job()
调用的代码可以在这里找到:
这个问题已经提出几个月了,但解决方案对我来说效果很好,让我不必编写自己的回退代码:
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中尝试了Google Cloud Translate),但它仍然有效。遇到了类似的问题,不想使用,因为我使用了多个翻译API,并且希望所有人的代码都保持不变 以下是不同翻译API的有效负载限制。这可能会影响节流(例如,使用较小的负载每秒多次调用Amazon Translate API工作正常):
- 亚马逊:
- 迪波尔:
- 谷歌云:
- Yandex云:
- Amazon没有说明普通API的节流限制是什么,文档只是说明: 亚马逊翻译的规模,以服务于客户的运营流量。如果遇到持续节流,请联系AWS支持
- 但一定要提到: AWS GovCloud(US)的默认限制设置为每个语言对每10秒5000字节,每个语言对每秒10个事务。您可以使用请求增加任何限制
ThrottlingException
)
如果我要更好地编写代码,我可能会像Marcin建议的那样,使用指数退避实现重试。或者,请求增加限额,以使我的生活更轻松,我的体验在API中更一致。通常用于处理此类错误。也许值得考虑一下?这不适合我的用例,因为无论如何都需要创建s3对象,使用指数退避重试解决了问题。是的,需要翻译的文本需要首先传输到s3,结果再次存储到s3。但是,根据用例的不同,这可以自动化,并且极大地增加了限制。