Getting a task by name from a task

How can I get the 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') 

This should be possible using 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?

+6
source share
1 answer

There is no guarantee that a task with this name exists - it may already be completed. And even if you manage to get task, it can be completed when you try to do something with it. Therefore, when you try to return it, you have no idea whether you add it for the first or second time.

Due to this uncertainty, I do not see any use case when getting a job by name can be useful.

EDIT:

You can specify the name of your task to ensure that a particular task is performed only once. When you add a task with a name to the queue, App Engine checks to see if there is a task with that name. If this happens, a subsequent attempt will fail.

For example, you can run many instances, and each instance may need to insert an object into the data warehouse. Your first option is to check if the object exists in the data store. This is a relatively slow operation, and by the time you received the answer (the entity does not exist) and decided to insert it, another instance might already have inserted it. Thus, you get two objects instead of one.

The second option is to use tasks. Instead of inserting a new object directly into the data warehouse, the instance creates a task to insert it and assigns a name to this task. 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 the object will be inserted only once.

+5
source

All Articles