【问题标题】:Google App Engine: Modifying 1000 entities using TaskQueueGoogle App Engine:使用 TaskQueue 修改 1000 个实体
【发布时间】:2014-07-15 15:09:46
【问题描述】:

我希望使用任务队列修改 1000 个实体,正如我在原始问题中建议的 Zig Mandel 在这里:Google App Engine: Modifying 1000 entities

我有一个像这样的 UserAccount 类型:

class UserAccount(ndb.Model):
    email = ndb.StringProperty()

一些 UserAccount emails 包含大写字母(例如:JohnathanDough@email.com),我想将email.lower() 应用于每个实体的电子邮件。

所以我设置了一个这样的任务队列:

class LowerEmailQueue(BaseHandler):

    def get(self):
        all_accounts = UserAccount.query().fetch()
        for a in all_accounts:
            taskqueue.add(url = '/lower-email', params = {'account_id': a.key.id()})


class LowerEmail(BaseHandler):

    def post(self):
        account_id = self.request.get('account_id')
        account = UserAccount.get_by_id(int(account_id))
        account.email = account.email.lower()
        account.put()

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/lower-email-queue', LowerEmailQueue),
    ('/lower-email', LowerEmail),


], debug=True)

我还没有运行它,因为我想防止对我的数据造成灾难性的破坏。这应该工作吗?

【问题讨论】:

    标签: python google-app-engine app-engine-ndb task-queue


    【解决方案1】:

    不,事实上这根本不会做任何事情,因为您不会对降低的电子邮件地址做任何事情。您需要将其实际分配回实体。

    account.email = account.email.lower()
    

    【讨论】:

    • 很好的发现丹尼尔。谢谢。假设这种变化,它会起作用吗?有人告诉我应该涉及游标。
    • 现在可以工作了,但是每个项目使用一个任务队列有点过头了。每个任务队列至少做 50 个。实际上你应该看看游标
    • 至少,您的循环可以构建小数组(比如 20 个项目),将数组字符串化并作为任务队列参数传递。我之前提到过游标,因为这将是最佳方式,查询由每个任务队列而不是循环进行。如果有更多的项目循环会从前端实例超时。
    • @ZigMandel,我对任务队列及其为每个任务队列设置 50 个的能力不太熟悉。如果不是太麻烦,您能否发布一个代码来举例说明您的建议?我想我可以从看到代码中学到最多。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多