我正在使用 Google App Engine 的 python API,我想做的是从服务器加载文件列表,如果它已经过去了超过一个时间间隔(例如 1 小时)。
为此,我尝试将执行的最后一个操作小时存储到文件中,并在下次读取它以了解与下一个请求的当前执行时间的差异,但后来我发现GAE不允许将文件写入磁盘,所以我不得不使用 blobstore。我尝试将已有的相同函数组合在一起,但使用 blobstore 函数,但即使创建文件,每次我尝试读取它时都会收到一条错误消息:
FinalizationError: ApplicationError: 101 Blobkey not found.
我不确定我所做的事情是错误的。我对 GAE 还很陌生,不知道这是否是完成我需要做的事情的最佳方法,并且我在 GAE 文档中找不到在同一脚本中读取和写入文件的示例文件 API。
预先感谢您能给我的任何提示。
这是我的代码的摘要版本, get 和 set 函数与代码中的一样:
# imports...
from datetime import datetime as time
from google.appengine.api import files
# create file
last_request_file = files.blobstore.create()
def update_list(time_interval):
# Get the time of the current request
current_time = time.now()
# Calculate timelapse between this an the last request
last_request = get_last_request_time(last_request_file)
elapsed_time = current_time - last_request
# If more than an interval update proxy list
if elapsed_time.total_seconds() >= time_interval:
# Request new list from the server
my_list = getList()
# Update last_request time
set_last_request(last_request_file)
return my_ist
def get_last_request_time(file_name):
request_time = None
with files.open(file_name, 'r') as f:
text_time = f.read()
if text_time:
request_time = time.strptime(text_time, '%Y-%m-%d %H:%M:%S')
else: # file was empty
request_time = time.min
# Finalize to close the file.
files.finalize(file_name)
return request_time
def set_last_request_time(file_name):
current_time = time.now()
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(time.strftime(current_time, '%Y-%m-%d %H:%M:%S')+'\n')
# Finalize to close the file.
files.finalize(file_name)
第一:为什么不将信息存储在任务队列中。您可以每小时运行一个任务队列来完成您的工作(获取列表)。
其次:只有在创建文件并写入文件时才使用 Finalize。但在文件完成后,您无法写入文件。要更新 blobstore 中的文件,您始终必须创建一个新文件。当您读取文件时,您不必最终确定它。要读取文件,您必须使用 blobreader。看:https://developers.google.com/appengine/docs/python/blobstore/blobreaderclass https://developers.google.com/appengine/docs/python/blobstore/blobreaderclass
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)