问题
I'm using the python API for Google App Engine, and what I am trying to do is load a list of files from a server if it has passed more than an interval of time (e.g 1 hour).
For this I tried to store the last operation hour of execution to a file and read it the next time to know the difference with the current time of execution of the next request, but then i found out that GAE does not allow to write files to disk, so I had to use the blobstore. I tried to put together the same functions i already had but using the blobstore functions, but even when the file is created, everytime i try to read it I get an error saying:
FinalizationError: ApplicationError: 101 Blobkey not found.
I'm not sure what I'm doing is wrong. I'm pretty new to GAE and don't know if this is the best approach to do what I need to do, and I couldn't find an example where you read and write files in the same script in the documentation of the GAE Files API.
Thanks in advance for any hints you can give me.
This is a summarized version of my code, the get and set functions are as they are in the code:
# 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)
回答1:
First: Why do'not store the information in a task queue. You can run a task queue every hour to do your job (get list).
Second: You only use finalize if you create a file and write to it. But you cannot write to a file, after it has been finalized. To update a file in the blobstore, you always have to create a new one. And when you read a file, you do not have to finalize it. To read a file you have to use a blobreader. See: https://developers.google.com/appengine/docs/python/blobstore/blobreaderclass
来源:https://stackoverflow.com/questions/12851419/writing-and-reading-blobstore-files-in-python-app-engine-api-to-store-timestamps