Skip to main content
 首页 » 编程设计

python之google.appengine.api.urlfetch 的截止日期是否限制为 60 秒

2024年10月17日27grandyang

我在 google app engine 上使用 python,并不断收到 google.appengine.api.urlfetch_errors.DeadlineExceededError 从一台做一些后端处理的机器发出的请求。这些请求大约需要 60 秒,有时会更长一些,所以我试图延长截止日期。

请求包含在重试中,从日志中我可以看到重试之间的时间始终约为 60 秒。我认为这是因为我配置不正确,或者误解了截止日期的限制。

机器配置是:

instance_class: B8 
basic_scaling: 
  max_instances: 1 
  idle_timeout: 10m 

我使用的代码是(为简单起见进行了编辑):

from google.appengine.api import urlfetch 
from retrying import retry 
 
timeout = 600 
retries = 10 
 
@retry( 
    stop_max_attempt_number=retries, 
    wait_exponential_multiplier=1000, 
    wait_exponential_max=1000*60*5 
) 
def fetch(url): 
    """Fetch remote data, retrying as necessary""" 
    urlfetch.set_default_fetch_deadline(timeout) 
    result = urlfetch.fetch(url) 
    if result.status_code != 200: 
        raise IOError("Did not receive OK response from server") 
    return result.content 
 
data = fetch(config['url']) 

我已经尝试将截止日期明确设置为 urlfetch.fetch(url, deadline=timeout) 但设置默认值似乎是大多数人建议的方法。

谁能澄清是否有可以为deadline设置的最大值?

请您参考如下方法:

请求计时器

Google App Engine 请求计时器 (Java/Python/Go) 确保请求具有有限的生命周期并且不会陷入无限循环。目前,前端实例请求的截止时间为 60 秒。 (后端实例没有相应的限制。)每个请求,包括预热(请求/_ah/warmup)和加载请求(“loading_request=1”日志头),都受此限制。

如果请求未能在 60 秒内返回并且抛出 DeadlineExceededError 但未被捕获,则请求将中止并返回 500 内部服务器错误。如果捕获到 DeadlineExceededError 但没有足够快地产生响应(您的时间少于一秒),则请求将中止并返回 500 内部服务器错误。

就我阅读文档而言,我认为应用引擎中的最大请求超时为 60 秒。 Here is the link to the documentation