简体   繁体   English

安排在Google App Engine中运行超过十分钟的任务的最佳方法是什么?

[英]What's the best way of scheduling a task that runs over ten minutes in Google App Engine?

Let's say I have a script that runs for an hour. 假设我有一个运行一个小时的脚本。 All it does is sleep for an hour and change something in the Datastore at the end of the hour. 它所做的只是睡眠一个小时,然后在小时结束时更改数据存储区中的某些内容。

def task(keyname):
    time.sleep(3600)
    q = Item.all()
    item = q.filter("keyname = ", "%s" % keyname).get()
    item.value += 1
    item.put()

What would be be the best way to accomplish this? 做到这一点的最佳方法是什么?

The cron documentation says that "an HTTP request invoked by cron can run for up to 10 minutes, but is subject to the same limits as other HTTP requests." cron文档说:“ cron调用的HTTP请求最多可以运行10分钟,但与其他HTTP请求一样,受到相同的限制。” I'm assuming this means cron tasks that run over ten minutes will break. 我假设这意味着运行十分钟的cron任务将中断。

I looked at the Task Queue documentation, but I don't want to queue these tasks one after another. 我查看了“任务队列”文档,但是我不想将这些任务一个接一个地排队。 I want them to happen concurrently. 我希望它们同时发生。

I was thinking of adding a counter property and running the task every minute instead of sleeping: 我正在考虑添加一个计数器属性并每分钟运行一次任务而不是睡觉:

def task(keyname):
    q = Item.all()
    item = q.filter("keyname = ", "%s" % keyname).get()
    if item.counter == 60:
        item.value += 1
        item.put()
    else:
        item.counter += 1
        item.put()

but that accesses the database way too many times. 但这太多次访问数据库。

So, does anyone have a smarter workaround? 那么,有没有人有一个更聪明的解决方法?

I don't understand what you mean when you say you want "these tasks" to happen concurrently; 我不明白您说要“这些任务”同时发生的意思。 you've only mentioned a single task. 您只提到了一项任务。

Regardless, the task queue is the way to do this. 无论如何,任务队列是执行此操作的方法。 You can use the countdown argument to the Task constructor to tell it to execute in 3600 seconds. 您可以使用Task构造函数的countdown参数告诉它在3600秒内执行。

I usually prefer to use the deferred library , which creates tasks dynamically without needing to set up specific handlers: 我通常更喜欢使用deferred ,该可以动态创建任务,而无需设置特定的处理程序:

from google.appengine.ext import deferred
...
deferred.defer(my_task, _countdown=3600)

The cron job hits a handler, and this handler starts a task. cron作业命中了一个处理程序,该处理程序启动了一个任务。 This process usually takes less than a second. 此过程通常不到一秒钟。 After that the task can run for as long as you need it - forever, if necessary. 之后,任务可以运行所需的时间,必要时可以永久运行。 It all depends on the target of a task . 这完全取决于任务的目标 10 minute limitation applies to front-end (F-type) instances only. 10分钟的限制仅适用于前端(F型)实例。

And you can run many tasks concurrently if necessary. 并且,如有必要,您可以同时运行许多任务。

@Soricidae, you're building a cron task. @Soricidae,您正在构建cron任务。

  1. For this you need to have a handler that calls your method task . 为此,您需要一个处理程序来调用您的方法task
  2. Also, you need to specify a URL route to that handler. 另外,您需要指定到该处理程序的URL路由。
  3. Then you need to specify in your cron.yaml a cronjob calling for the task method. 然后,您需要在cron.yaml中指定调用任务方法的cronjob。

*. *。 Additionally I deeply suggest using taskqueues in order for the appengine to solve your load time issues during the cron task. 另外,我强烈建议使用任务队列,以便Appengine在cron任务期间解决您的加载时间问题。

For example, in my case I have a welcome handler that emails users, checking every 24 hours for new users with more than 1 day that have not yet been welcomed... 例如,在我的情况下,我有一个欢迎处理程序,可以向用户发送电子邮件,每隔24小时检查一次超过1天仍未受到欢迎的新用户...

So, I've defined the following routes (1 for cronjob, 1 for taskqueue): 因此,我定义了以下路由(1个用于cronjob,1个用于taskqueue):

RedirectRoute('/cronjob-welcome/', handlers.WelcomeCronjobHandler, name='cronjob-welcome', strict_slash=True),
RedirectRoute('/taskqueue-welcome/', handlers.WelcomeHandler, name='taskqueue-welcome', strict_slash=True),

Then, I've defined a cronjob handler that once called it goes for the real operations handler as a taskqueue, see: 然后,我定义了一个cronjob处理程序,一旦调用它,它就会作为任务队列用于实际操作处理程序,请参阅:

class WelcomeCronjobHandler(BaseHandler):
    def get(self):
        welcome_url = self.uri_for('taskqueue-welcome')
        taskqueue.add(url=welcome_url, params={
            'offset': 0
        })

You can see that this handler asks for welcome_url, which calls the defined handler taskqueue-welcome in routes and does the following: 您可以看到此处理程序询问了welcome_url,它会在路由中调用已定义的处理程序taskqueue-welcome并执行以下操作:

class WelcomeHandler(BaseHandler):
    """
    Core Handler for sending users welcome message
    Use with TaskQueue
    """

    @taskqueue_method
    def post(self):
        # ... MY CODE FOR EMAILING USERS ....
        # ... YOUR CODE SHOULD GO IN HERE ...

Finally, in order for the cronjob handler to be called every 24 hours, in my cron.yaml file I've added the following, notice the route for cronjob-welcome : 最后,为了使cronjob处理程序每​​24小时调用一次,在我的cron.yaml文件中添加了以下内容,请注意cronjob-welcome的路由:

- description: welcome messaging
  url: /cronjob-welcome/
  schedule: every day 13:00

Summarizing, my cron.yaml calls every day at 13:00 hours for the cronjob handler , which at the same time triggers a taskqueue handler , which dispatches a google appengine taskqueue solving my scheduled task . 总而言之,我的cron.yaml每天在13:00时为cronjob处理程序进行调用,该处理程序同时触发一个任务队列处理程序 ,该任务 处理程序调度一个Google appengine 任务队列来解决我的计划任务

Hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过AJAX将用户输入的文本发送到Google App Engine的最佳方法是什么? - What's the best way to send user-inputted text via AJAX to Google App Engine? app-engine-patch已经死了。 现在,在Google App Engine上使用Django的最佳方式是什么? - app-engine-patch is dead. Now what is the best way to use Django on Google App Engine? 在 Google App Engine 中查找相对路径的好方法是什么? - What's a good way to find relative paths in Google App Engine? 在Google App Engine中填充许多延迟任务的任务队列的最佳存储桶大小是多少? - What is the best bucket size for a task queue filled with many deferred tasks in Google App Engine? 什么是最好的Google App Engine blobstore工作流程? - What is the best Google App Engine blobstore workflow? 在python中为google app引擎网站创建系统属性集合的最佳方法是什么? - What is the best way to create a collection of system properties for a google app engine website in python? 将Django应用转换为Google App Engine的最佳方法是什么? - What are the best approaches to convert Django app to Google App Engine? 有没有更好的方法来遍历Google App Engine中(Django)模板中的元组? - Is there a better way to iterate over tuples in a (Django) template in Google App Engine? 在Google App Engine上从登录页面存储电子邮件的最佳方法? - Best way to store emails from a landing page on google app engine? 在python中为谷歌应用引擎创建枚举模型类的最佳方法? - Best way to create an enum model class for google app engine in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM