在任务队列为空之后调用脚本

编程入门 行业动态 更新时间:2024-10-24 18:28:15
本文介绍了在任务队列为空之后调用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在运行一个脚本来启动几个任务:

对于范围(0,5)中的我: taskqueue.add(url ='/ example', params = {'num':i})

据我所知,这些任务是并行运行的。无论如何,我可以告诉AppEngine运行一个特定的任务/ Python文件,一旦我刚刚插入队列的所有任务都完成了吗?我想为上一次循环迭代中调用的任务发送一个标志,但是如果任务并行运行,则不会保证一旦完成,其他部分也完成了。

感谢,

Joel

解决方案

你发起任务,你知道会有多少。使用期望计数或任务列表将实体插入数据存储区。然后使用计数器或标记来指示任务何时运行。粗略地说,这个过程可能如下所示:

新种类:

class TaskBatch(db.Model): expected_count = db.InteProProperty() $ b $ class TaskMarker(db.Model): pass

count = 5 taskbatch = TaskBatch(expected_count = count).put()在范围内(5): taskqueue.add(url ='/ example ', params = {'num':i,'batch':str(taskbatch)})

$ p $ b $ pre $ def $($) $ = $ .request.get('num')#做你的东西.... batch = self.request.get('batch') TaskMarker(key_name = num ,parent = db.Key(batch)) taskqueue.add(url ='/ example / isdone', params = {'num':i,'batch':str(taskbatch)})

isdone 任务可能如下所示:

def post(self): num = self.request.get('num') batch_key = db.Key(self.request.get('batch')) batch marker_keys = [db.Key.from_path('TaskMarker',i,parent = batch) for i in range(batch.expected_count)] markers = db.get(marker_keys)如果所有(标记):#完成您的操作。

具体过程会根据您的用例的具体情况而略有不同,例如插入多少个任务。

I am running a script which initiates several tasks:

for i in range (0,5): taskqueue.add(url='/example', params={'num': i})

As far as I understand, the tasks are running in parallel. Is there anyway I can tell AppEngine to run a specific task/python file once all the tasks I just inserted to the queue are ALL finished? I thought about sending a flag to the task that was called in the last loop iteration, but if tasks run in parallel it is not guarnteed that once it is finished, the other ones were finished too.

Thanks,

Joel

解决方案

When you initiate the tasks, you know how many there will be. Insert an entity into the datastore with the 'expected' count, or list of tasks. Then use either counters or markers to indicate when a task has run. Roughly, the process might look something like:

The new kinds:

class TaskBatch(db.Model): expected_count = db.IntegerProperty() class TaskMarker(db.Model): pass

Then, adjust your calling routine to do something like:

count = 5 taskbatch = TaskBatch(expected_count=count).put() for i in range(5): taskqueue.add(url='/example', params={'num': i, 'batch': str(taskbatch)})

And, at the end of your tasks:

def post(self): num = self.request.get('num') # do your stuff.... batch = self.request.get('batch') TaskMarker(key_name=num, parent=db.Key(batch)) taskqueue.add(url='/example/isdone', params={'num': i, 'batch': str(taskbatch)})

And the isdone task could look something like:

def post(self): num = self.request.get('num') batch_key = db.Key(self.request.get('batch')) batch = TaskBatch.get(batch_key) marker_keys = [db.Key.from_path('TaskMarker', i, parent=batch) for i in range(batch.expected_count)] markers = db.get(marker_keys) if all(markers): # do your done action.

The exact process will vary slightly depending on the specifics of your usecase, such as how many tasks your inserting.

更多推荐

在任务队列为空之后调用脚本

本文发布于:2023-11-12 01:37:45,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   为空   脚本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!