Google应用引擎任务队列获取统计信息fai

2024-09-28 22:21:51 发布

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

我有一个由后端提供服务的请求队列,当队列为空时,我需要触发另一个脚本。在

目前,我在从队列中租赁任务的方法中使用了一个非常粗糙的检测,因此如果返回的任务列表为空,我们就假定没有更多的任务可以租赁并触发下一步。然而,虽然这在大多数情况下都是有效的,但是偶尔租约请求似乎返回一个空列表,即使有任务可用。在

总之,我认为更好的方法是使用队列的fetch_statistics方法。这样,脚本就可以监视请求队列中发生的事情,并知道队列中不再有项目了。现在,这显然可以通过restapi用于队列,但是在内部使用时,使用它似乎有些落后。在

所以我在做Queue.fetch_统计信息()调用,但它抛出错误。我试着把这个错误输入到Google中,但是没有返回任何结果。stackoverflow也是。在

它总是抛出:

AttributeError: type object 'QueueStatistics' has no attribute '_QueueStatistics__TranslateError'

我的代码是:

^{pr2}$

有人能解释一下吗?我是不是在做一些很蠢的事?在


Tags: 项目方法脚本restapi列表队列queue错误
3条回答

任务队列统计API现在已经有文档记录并可公开使用。错误不再发生。在

为了避免对其他人有用,这里有一个示例函数,可以让您开始从应用程序获取队列信息。这只是一个例子,可以更好地处理错误,但它应该能让您启动并运行。以前我们使用过Taskqueue客户机,但我认为这有点过头了,因为我们可以在代码中租赁和删除,所以我使用了app identity,而且效果很好。在

from google.appengine.api import taskqueue
from google.appengine.api import app_identity
from google.appengine.api import urlfetch
try:
    import json
except ImportError:
    import simplejson as json
import logging

def get_queue_info(queue_name, stats=False):
    '''
        Uses the Queue REST API to fetch queue info
        Args:
            queue_name: string - the name of the queue
            stats: boolean - get the stats info too
        RETURNS:
            DICT: from the JSON response or False on fail
    '''
    scope = 'https://www.googleapis.com/auth/taskqueue'
    authorization_token, _ = app_identity.get_access_token(scope)
    app_id = app_identity.get_application_id()
    #note the s~ denoting HRD its not mentioned in the docs as far as 
    #I can see, but it wont work without it
    uri = 'https://www.googleapis.com/taskqueue/v1beta1/projects/s~%s/taskqueues/%s?getStats=%s' % (app_id, queue_name, stats)
    #make the call to the API
    response = urlfetch.fetch(uri, method="GET", headers = {"Authorization": "OAuth " + authorization_token})
    if response.status_code == 200:
        result = json.loads(response.content)
    else:
        logging.error('could not get queue')
        logging.error(response.status_code)
        logging.error(response.content)
        return False


    return result

别忘了更新你的队列.yaml应用程序标识的acl

^{pr2}$

我希望有人发现这个有用。在

同时,我已经发布了一个特性请求,这样我们就可以用Queue对象来完成这个任务,如果你也想要的话,请去给它加上星号。http://goo.gl/W8Pk1

你得到的具体错误的直接原因似乎是代码中的一个错误;Queue.fetch_统计信息电话()QueueStatistics.fetch()调用QueueStatistics.\u FetchMultipleQueues(),它显然遇到了apiproxy_错误.ApplicationError然后尝试调用cls.\uu TranslateError(),但QueueStatistics类上没有此类方法。在

我不知道ApplicationError的深层原因,但这可能意味着生产运行时还不支持该功能。在

相关问题 更多 >