将文件直接写入blob时,App Engine出现不稳定的问题

2024-04-26 11:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中使用appengine。为了存储用户的图像,我将它们直接写入blobstore,如Google documentation所示。在

我的代码如下:

# Image insertion in the blobstore
file_name = files.blobstore.create(mime_type='image/jpeg')
with files.open(file_name, 'a') as f:
    f.write(self.imageContent)
files.finalize(file_name)
self.blobKey = files.blobstore.get_blob_key(file_name)
logging.info("Blobkey: "+str(self.blobKey))

这个问题不稳定。我什么也没改变,从昨天开始,有时它有用,有时不起作用。为什么?当我打印blobkey(代码的最后一行)时,我可以看到图像是否已保存到blobstore中。在

当它工作时,我会显示以下行:

^{pr2}$

当它不起作用时,我会在日志中记录:

Blobkey: None

最后一个细节:图像(self.imageContent)在每次写入之前都要进行预处理并转换为.JPEG。在

编辑:
每次,图像都存储在blobstore中(我可以在管理控制台的blobviewer中看到它们)。这就是get_blob_key函数出现故障。。。在

我想知道在这种情况下我该怎么办?我是不是做错了什么让应用引擎的行为变得不稳定。我怎么解决这个问题?在


Tags: key代码name图像selfgetfilesblob
1条回答
网友
1楼 · 发布于 2024-04-26 11:32:00

我终于设法解决了这个问题,让线程在50毫秒的间隔内休眠

这是我添加的代码:

# Sometimes blobKey is None
self.blobKey = files.blobstore.get_blob_key(file_name)

# We have to make it wait til it works!
for i in range(1,3):
    if(self.blobKey):
         break
    else:
        logging.info("blobKey is still None")
        time.sleep(0.05)
        self.blobKey = files.blobstore.get_blob_key(file_name)

logging.info("Blobkey: "+str(self.blobKey))

当然,您必须导入时间模块才能使其工作。在

^{pr2}$

我和systempuntoout提到的4872期的人做的差不多。在

谢谢。请随时添加任何建议。在

相关问题 更多 >