Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting task by name from taskqueue

How can I get a task by name?

from google.appengine.api import taskqueue
taskqueue.add(name='foobar', url='/some-handler', params={'foo': 'bar'}

task_queue = taskqueue.Queue('default')
task_queue.delete_tasks_by_name('foobar') # would work

# looking for a method like this:
foobar_task = task_queue.get_task_by_name('foobar')

It should be possible with the REST API (https://developers.google.com/appengine/docs/python/taskqueue/rest/tasks/get). But I would prefer something like task_queue.get_task_by_name('foobar'). Any ideas? Did I miss something?

like image 391
mattes Avatar asked Mar 24 '14 22:03

mattes


People also ask

How does TaskRouter determine which workers are associated with a TaskQueue?

TaskRouter binds Workers to TaskQueues using the TaskQueue's TargetWorkers expression. TargetWorkers uses an SQL-like expression syntax to filter the Workers in the Workspace. And only Bob would receive tasks from this TaskQueue.

Is task queue asynchronous?

Task queue (or) Job queue plays an important role in a web application where it schedules programs (or) jobs to the queue for batch processing. This enables services (or) programs to execute in the background without disturbing other processes. This Task Queue is designed for asynchronous work.

What is a task queue?

Task queues let applications perform work, called tasks, asynchronously outside of a user request. If an app needs to execute work in the background, it adds tasks to task queues. The tasks are executed later, by worker services. The Task Queue service is designed for asynchronous work.


1 Answers

There is no guarantee that the task with this name exists - it may have been already executed. And even if you manage to get a task, it may be executed while you are trying to do something with it. So when you try to put it back, you have no idea if you are adding it for the first time or for the second time.

Because of this uncertainty, I can't see any use case where getting a task by name may be useful.

EDIT:

You can give a name to your task in order to ensure that a particular task only executes once. When you add a task with a name to a queue, App Engine will check if the task with such name already exists. If it does, the subsequent attempt will fail.

For example, you can have many instances running, and each instance may need to insert an entity in the Datastore. Your first option is to check if an entity already exists in a datastore. This is a relatively slow operation, and by the time you received your response (entity does not exist) and decide to insert it, another instance could have already inserted it. So you end up with two entities instead of one.

Your second option is to use tasks. Instead of inserting a new entity directly into a datastore, an instance creates a task to insert it, and it gives this task a name. If another instance tries to add a task with the same name, it will simply override the existing task. As a result, you are guaranteed that an entity will be inserted only once.

like image 147
Andrei Volgin Avatar answered Oct 04 '22 22:10

Andrei Volgin