【问题标题】:Is possible to avoid the 60 seconds limit in urllib2.urlopen with GAE?是否可以使用 GAE 避免 urllib2.urlopen 中的 60 秒限制?
【发布时间】:2016-06-09 21:41:02
【问题描述】:

我正在使用 urllib2.urlopen 从慢速服务器请求一个大小约为 14MB 的文件,它花费了 60 多秒来获取数据,我收到了错误:

等待来自 URL 的 HTTP 响应时已超过截止日期: http://bigfile.zip?type=CSV

这是我的代码:

class CronChargeBT(webapp2.RequestHandler):
    def get(self):
        taskqueue.add(queue_name = 'optimized-queue', url='/cronChargeBTB')

class CronChargeBTB(webapp2.RequestHandler):
    def post(self):
        url = "http://bigfile.zip?type=CSV"

        url_request = urllib2.Request(url)
        url_request.add_header('Accept-encoding', 'gzip')

        urlfetch.set_default_fetch_deadline(300)
        response = urllib2.urlopen(url_request, timeout=300)

        buf = StringIO(response.read())
        f = gzip.GzipFile(fileobj=buf)
        ...work with the data insiste the file...

我创建了一个调用 CronChargeBT 的 cron 任务。这里是 cron.yaml:

- description: cargar BlueTomato
  url: /cronChargeBT
  target: charge
  schedule: every wed,sun 01:00

它创建一个新任务并插入一个队列,这里是队列配置:

- name: optimized-queue
  rate: 40/s
  bucket_size: 60
  max_concurrent_requests: 10
  retry_parameters:
    task_retry_limit: 1
    min_backoff_seconds: 10
    max_backoff_seconds: 200

当然,timeout=300 不起作用,因为 GAE 中的 60 秒限制,但我认为我可以使用任务来避免它...任何人都知道我如何获取文件中的数据以避免此超时。

非常感谢!!!

【问题讨论】:

    标签: python-2.7 google-app-engine urllib2


    【解决方案1】:

    Cron 作业的截止时间限制为 10 分钟,而不是 60 秒。如果您的下载失败,也许只是重试?如果您从计算机下载,下载是否有效?如果您下载的服务器太慢或不稳定,您在 GAE 上无能为力。

    编辑:根据https://cloud.google.com/appengine/docs/java/outbound-requests#request_timeouts,cron 作业请求的最大截止时间为 60 秒。因此,您无法绕过它。

    【讨论】:

    • 谢谢@kanghj,我认为我得到的超时错误与 urllib2.request 相关,而不是 cron 作业。我可以在不到 10 分钟,也许 3 分钟内从我的计算机下载文件,并且代码在我的开发机器上运行良好
    • 根据cloud.google.com/appengine/docs/java/…,cron 作业请求的最大截止时间为 60 秒。这可能是你的瓶颈。这是你观察到的吗?
    • 实际上你无法绕过它,因为它是 GAE 本身施加的限制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多