写入云端存储时GAE内存泄漏,我可以流式传输吗?

Jab*_*key 7 python google-app-engine memory-leaks google-cloud-storage

我正在从外部API请求数据,并将其直接写入云存储文件.数据写得很好,但是当我扩展时,我遇到内存问题并且在GAE实例上达到了1024 MB的限制.

这些是我得到的关键错误:

Exceeded soft private memory limit of 1024 MB with 1425 MB after servicing 46 requests total
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码的摘要:

import cloudstorage as gcs
import urllib2

# Example file path
filePath = '/bucket/dir/file.gzip'

def deferrableTask(filePath, api_url, post_body):
  with gcs.open(filePath, 'w') as f:
      request = urllib2.Request(api_url, post_body)
      try:
        response = urllib2.urlopen(request, timeout = 600)
      except urllib2.HTTPError, e:
        raise customError(e)
      else:
        while True:
          chunk = response.read(16 * 1024)
          if not chunk: break
          f.write(chunk)
      f.close()
      del f
  gc.collect()
Run Code Online (Sandbox Code Playgroud)

使用任务队列可以推断上述任务.最多可能有40个并发运行在队列中.在我app.yaml,我有以下设置:

instance_class: F4_1G
automatic_scaling:
  max_concurrent_requests: 4
Run Code Online (Sandbox Code Playgroud)

此代码适用于将api数据写入云存储.当我开始做几百个这样的请求时,我开始遇到内存问题.

请求的gzip文件大小从300 kb到10-20 Mb,我认为通过使用gc.collect(),结合限制并发实例请求的数量,足以减少内存泄漏.我也知道urllib2只是app引擎urlfetch的包装器,但是提取不是问题,而是缩放.

变量f需要多少内存?是否可以直接流式传输到Google云端存储,而不是先将数据加载到实例内存中?

Sho*_*hit 0

有一种方法,您可以直接从外部应用程序将数据写入GCS,为此您必须将存储桶的ACL更改为公共,然后借助云存储API将数据写入存储桶。

另外,请告诉我您是否错过了问题中自动缩放配置的任何细节。如果是,请指定这些详细信息。