在Python App Engine API中编写和读取blobstore文件以存储时间戳 [英] Writing and reading blobstore files in Python App Engine API to store timestamps

查看:80
本文介绍了在Python App Engine API中编写和读取blobstore文件以存储时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Google App Engine使用python API,并且如果它已超过时间间隔(例如1小时),则从服务器加载文件列表。



为此,我尝试将最后一次执行操作时间存储到一个文件中,并在下一次读取它以了解与当前执行下一个请求的时间不同之处,但之后我发现GAE不允许将文件写入磁盘,所以我必须使用blobstore。我尝试将我已有的相同函数放在一起,但使用blobstore函数,但即使在创建文件时,每当我尝试读取它时,我都会收到错误消息:

  FinalizationError:ApplicationError:101找不到Blobkey。 

我不确定我在做什么是错误的。我对GAE相当陌生,不知道这是否是做我需要做的最好的方法,而且我找不到在GAE文档中的同一脚本中读写文件的示例Files API。



感谢您提供给我的任何提示。



这是一个概要版本我的代码,get和set函数就像它们在代码中一样:

 #imports ... 
from datetime将datetime作为时间
from google.appengine.api导入文件

创建文件
last_request_file = files.blobstore.create()


def update_list(time_interval):
#获取当前请求的时间
current_time = time.now()

#计算最后一次请求之间的时间间隔
last_request = get_last_request_time(last_request_file)
elapsed_time = current_time - last_request

#如果超过间隔更新代理列表
if elapsed d_time.total_seconds()> = time_interval:
#从服务器请求新列表
my_list = getList()

#更新last_request时间
set_last_request(last_request_file)

返回my_ist


def get_last_request_time(file_name):
request_time = None

with files.open(file_name, '')作为f:
text_time = f.read()
if text_time:
request_time = time.strptime(text_time,'%Y-%m-%d%H:% M:%S')
else:#文件为空
request_time = time.min

#结束关闭文件。
files.finalize(file_name)

return request_time

$ b def set_last_request_time(file_name):
current_time = time.now()

#打开文件并用files.open(file_name,'a')将文件写入
作为f:
f.write(time.strftime(current_time,'%Y - %m-%d%H:%M:%S')+'\\\
')

#结束关闭文件。
files.finalize(file_name)


解决方案

:为什么不将信息存储在任务队列中。您可以每小时运行一次任务队列来完成您的工作(获取列表)。

第二:如果您创建文件并写入文件,则只能使用finalize。但是在完成之后,你无法写入文件。要更新blobstore中的文件,您必须始终创建一个新文件。而当你阅读一个文件时,你不必确定它。要阅读一个文件,你必须使用一个Blobreader。请参阅: https://developers.google.com/appengine/docs/python/blobstore / blobreaderclass

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)

解决方案

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

这篇关于在Python App Engine API中编写和读取blobstore文件以存储时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆