【问题标题】:GAE - Upload optimized image to cloud storageGAE - 将优化图像上传到云存储
【发布时间】:2015-05-19 13:01:53
【问题描述】:

我正在开发一个简单的应用程序,它可以拍摄图像并优化它们并将它们保存在云存储中。我找到了一个获取文件并使用 PIL 对其进行优化的示例。代码如下所示:

def inPlaceOptimizeImage(photo_blob):
        blob_key = photo_blob.key()
        new_blob_key = None

        img = Image.open(photo_blob.open())
        output = StringIO.StringIO()
        img.save(output,img.format, optimized=True,quality=90)
        opt_img = output.getvalue()
        output.close()

        # Create the file
        file_name = files.blobstore.create(mime_type=photo_blob.content_type)

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(opt_img)

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        return files.blobstore.get_blob_key(file_name)

这在本地运行良好(尽管我不知道它的优化程度,因为当我通过http://www.jpegmini.com/ 之类的东西运行上传的图像时,它仍然减少了 2.4 倍)。但是,当我部署应用程序并尝试上传图像时,我经常在日志中收到 500 个错误和这些消息:

F 00:30:33.322 Exceeded soft private memory limit of 128 MB with 156 MB after servicing 7 requests total
W 00:30:33.322 While handling this request, the process that handled this request was found to be using too much memory and was terminated. This is likely to cause a new process to be used for the next request to your application. If you see this message frequently, you may have a memory leak in your application.

我有两个问题:

  1. 这甚至是优化图像并将其保存在云存储中的最佳方式吗?
  2. 如何防止这 500 个错误发生?

提前致谢。

【问题讨论】:

    标签: python google-app-engine google-cloud-storage image-optimization


    【解决方案1】:

    您遇到的错误是由于您的 Instance 类的内存限制而发生的。

    我建议您编辑 .yaml 文件以配置您的模块,并将您的 Instance 类指定为 F2 或更高版本。 如果您不使用模块,您还应该在 app.yaml 文件的开头添加“module: default”,让 GAE 知道这是您的默认模块。

    您可以查看文档中的 article 以查看可用的不同实例类以及轻松配置它们的方法。

    另一个更基本的解决方法是在上传图片时限制图片大小,但您最终会遇到类似的问题。

    关于前面的问题和优化图像的方法,您可能需要查看App Engine Images API,它提供了使用专用图像服务处理图像数据的能力。在您的情况下,您可能喜欢“手气不错”的转变。通过使用此 API,您可能不需要更新您的 Instance 类。

    【讨论】:

    • 谢谢!我正在努力将我的应用程序分解为模块,我将对此进行测试。
    猜你喜欢
    • 2015-01-24
    • 2018-02-15
    • 2014-05-07
    • 2021-10-28
    • 2019-08-15
    • 2021-04-21
    • 2015-02-28
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多