如何在GAE中使用推送队列(任务)? [英] How to use push queues (tasks) with GAE?

查看:115
本文介绍了如何在GAE中使用推送队列(任务)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GAE应用程序应该将多个文件上传到另一台服务器(使用urlfetch)。
如何通过实施任务,假设在上次任务完成后我应该再执行一次动作?

My GAE application should upload several files to another server (with urlfetch usage). How to implement this with tasks with assumption that at the end of last task completion I should perform one more action?

我怎么知道最后一项任务何时完成?

How can I know when the last task is completed?

更新。使用任务的以下方法是否正确?

Upd. Is the following approach with Tasks correct?

class Accumulator(db.Model):
    counter = db.IntegerProperty()

def increase_counter(key):
    obj = db.get(key)
    obj.counter += 1
    obj.put()

def zero_counter(key):
    obj = db.get(key)
    obj.counter = 0
    obj.put()

def decrease_counter(key):
    obj = db.get(key)
    obj.counter -= 1
    obj.put()

def get_counter(key):
    obj = db.get(key)
    return obj.counter

class PublishPhotosHandler(webapp.RequestHandler):
    # where this tasks_counter should be defined? seems not here
    db.run_in_transaction(zero_counter, some_unique_key)
    for argument in files_arguments:
        taskqueue.add(url='/upload', params={'key': key})
        db.run_in_transaction(increase_counter, some_unique_key)

    # here we redirect user to another page '/checkstatus'
    ...
    # nothing is shown to the user here

class UploadWorker(webapp.RequestHandler):
    def post(self):
        key = self.request.get('key')
        result = urlfetch.fetch(...)
        db.run_in_transaction(decrease_counter, some_unique_key)
        # how to return there error, so the task will be retried?
        # nothing is shown to the user here

# this is our '/checkstatus' page
class CheckStatus(webapp.RequestHandler):
    def get(self, key):
        if get_counter(some_unique_key) == 0:
            # all tasks finished
            # show the content


推荐答案

您可以创建一个 counter 实体,该实体存储要执行的任务的当前计数。和每个任务。完成后,必须降低此柜台的价值,交易中。如果它变为0,那么它是最后一个动作,刚刚完成。

You can make an counter entity, that stores current count of tasks to execute. And each task. after finish, must decrease value of this counter, in transaction. If it becomes 0, then it's last action, just finished.

这篇关于如何在GAE中使用推送队列(任务)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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