python - 在 Python 2.5 中将 Google App Engine blobstore 作为文件写入的正确方法是什么

标签 python google-app-engine

当我尝试对 Google App Engine blobstore 进行简单写入时,我目前超出了软内存限制。编写此代码以使其不泄漏内存的正确方法是什么?

from __future__ import with_statement
from google.appengine.api import files
from google.appengine.api import blobstore
def files_test(limit):
file_name = files.blobstore.create(mime_type='application/octet-stream') 
   try:
     with files.open(file_name, 'a') as f:
       for x in range(limit):
         f.write("Testing \n")

   finally:
     files.finalize(file_name)
     return files.blobstore.get_blob_key(file_name)

files_test(4000) 产生错误:

在为总共 27 个请求提供服务后,超过了 157.578 MB 的软专用内存限制

最佳答案

不幸的是,python 的垃圾收集器并不完美。您所做的每一次写入都会创建许多小对象(通过 Protocol Buffer 创建),由于某种原因,python 不会即时收集这些对象。我发现在 mapreduce 库中我必须做

import gc
gc.collect()

不时让垃圾收集器开心。

关于python - 在 Python 2.5 中将 Google App Engine blobstore 作为文件写入的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210731/

相关文章:

node.js - 在 Google Cloud App Engine 上部署 Angular Universal 应用程序时出错

python - 从本地 GAE 项目连接到 Google Cloud Datastore

python - 字谜代码错误 : python

python - 在 Pandas 中,如何用数据帧的另一个子集替换数据帧的一个子集?

python - 在python(win10)中导入模块rospy的问题

python - 如何从 Google AppEngine 应用程序中获取该应用程序的应用程序标题

python - 如何在GAE上处理大文件?

python - Python 中的命令输出解析

python - 提取字母并填充单词的位置

java - 使用 Blobstore API 将文件上传到谷歌云存储