我正在开发一个简单的应用程序,它可以拍摄图像并优化它们并将它们保存在云存储中。我找到了一个获取文件并使用 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.
我有两个问题:
- 这甚至是在云存储中优化和保存图像的最佳方式吗?
- 如何防止这些 500 错误发生?
提前致谢。