Google App Engine

Using Pull Queues in Python

Pull queues allow you to design your own system to consume App Engine tasks. The task consumer can be part of your App Engine app (such as a backend) or a system outside of App Engine (using the Task Queue REST API). The task consumer leases a specific number of tasks for a specific duration, then processes and deletes them before the lease ends.

Using pull queues requires your application to handle some functions that are automated in push queues:

  • Your application needs to scale the number of workers based on processing volume. If your application does not handle scaling, you risk wasting computing resources if there are no tasks to process; you also risk latency if you have too many tasks to process.
  • Your application also needs to explicitly delete tasks after processing. In push queues, App Engine deletes the tasks for you. If your application does not delete pull queue tasks after processing, another worker might re-process the task. This wastes computing resources and risks errors if tasks are not idempotent.

Pull queues require a specific configuration in queue.yaml. For more information, please see Defining Pull Queues on the Task Queue configuration page.

The following sections describe the process of enqueuing, leasing, and deleting tasks using pull queues.

  1. Pull queue overview
  2. Pulling tasks within App Engine
  3. Pulling tasks to a backend
  4. Pulling tasks from outside App Engine
  5. Quotas and limits for pull queues

Pull queue overview

Pull queues allow a task consumer to process tasks outside of App Engine's default task processing system. If the task consumer is a part of your App Engine app, you can manipulate tasks using simple API calls from the google.appengine.api.taskqueue module. Task consumers outside of App Engine can pull tasks using the Task Queue REST API.

The process works like this:

  1. The task consumer leases tasks, either via the Task Queue API (if the consumer is internal to App Engine) or the Task Queue REST API (if the consumer is external to App Engine).
  2. App Engine sends task data to the consumer.
  3. The consumer processes the tasks. If the task fails to execute before the lease expires, the consumer can lease it again. This counts as a retry attempt, and you can configure the maximum number of retry attempts before the system deletes the task.
  4. Once a task executes successfully, the task consumer must delete it.
  5. The task consumer is responsible for scaling instances based on processing volume.

Pulling tasks within App Engine

You can use pull queues within the App Engine environment using simple API calls to add tasks to a pull queue, lease them, and delete them after processing.

Before you begin, make sure to configure the pull queue in queue.yaml.

Adding tasks to a pull queue

To add tasks to a pull queue, simply get the queue using the queue name defined in queue.yaml, and set the Task method to PULL. The following example enqueues tasks in a pull queue named pull-queue:

from google.appengine.api import taskqueue

q = taskqueue.Queue('pull-queue')
tasks = []
payload_str = 'hello world'
tasks.append(taskqueue.Task(payload=payload_str, method='PULL'))
q.add(tasks)

Leasing tasks

Once you have added tasks to a pull queue, you can lease one or more tasks using lease_tasks(). There may be a short delay before tasks recently added using add() become available via lease_tasks(). When you request a lease, you specify the number of tasks to lease (up to a maximum of 1,000 tasks) and the duration of the lease in seconds (up to a maximum of one week). The lease duration needs to be long enough to ensure that the slowest task will have time to finish before the lease period expires. You can extend a task lease using modify_task_lease().

Leasing a task makes it unavailable for processing by another worker, and it remains unavailable until the lease expires. If you lease an individual task, the API selects the task from the front of the queue. If no such task is available, an empty list is returned.

This method returns a Task object containing a list of tasks leased from the queue.

from google.appengine.api import taskqueue

q = taskqueue.Queue('pull-queue')
q.lease_tasks(3600, 100)

Experimental!

Task Tagging is an experimental, innovative, and rapidly changing new feature for Google App Engine. Unfortunately, being on the bleeding edge means that we may make backwards-incompatible changes to Task Tagging. We will inform the community when this feature is no longer experimental.
 


Not all tasks are alike; your code can "tag" tasks and then choose tasks to lease by tag. The tag acts as a filter.

from google.appengine.api import taskqueue

q = taskqueue.Queue('pull-queue')
q.add(taskqueue.Task(payload='parse1', method='PULL', tag='parse'))
q.add(taskqueue.Task(payload='parse2', method='PULL', tag='parse'))
q.add(taskqueue.Task(payload='render1', method='PULL', tag='render'))
q.add(taskqueue.Task(payload='render2', method='PULL', tag='render'))

q.lease_tasks_by_tag(3600, 100, 'render') # leases render tasks, but not parse

q.lease_tasks_by_tag(3600, 100) # Leases up to 100 tasks that have same tag.
                                # Tag is that of "oldest" task by eta.

Deleting tasks

In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing them, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.

You can delete a list of tasks, such as that returned by lease_task(), simply by passing it to delete_tasks():

from google.appengine.api import taskqueue

q = taskqueue.Queue('pull-queue')
tasks = q.lease_tasks(3600, 100)
# Perform some work with the tasks here
q.delete_tasks(tasks)

Pulling tasks to a backend

You can use App Engine Backends as workers to lease and process pull queue tasks. Backends allow you to process more work without having to worry about request deadlines and other restrictions normally imposed by App Engine. Using backends with pull queues gives you processing efficiencies by allowing you to batch task processing using leases.

For more information about using backends, check out the Backends documentation.

Pulling tasks from outside App Engine

If you need to use pull queues from outside App Engine, you must use the Task Queue REST API. The REST API is a Google web service accessible at a globally-unique URI of the form:

https://www.googleapis.com/taskqueue/v1beta2/projects/taskqueues

Google provides the following client libraries that you can use to call the Task Queue methods remotely:

In the tables below, the first column shows each library's stage of development; note that some are still in early stages. The second column links to the main page for each library.

The set of mature libraries includes:

Client library Documentation All client library samples
Google APIs Client Library for .NET Google APIs Client Library for .NET .NET samples
Google APIs Client Library for Java (rc) google-api-java-client/ Java samples
Google APIs Client Library for JavaScript (beta) Google APIs Client Library for JavaScript JavaScript samples
Google APIs Client Library for Objective-C google-api-objectivec-client/ Objective-C samples
Google APIs Client Library for PHP (beta) google-api-php-client PHP samples
Google APIs Client Library for Python Google APIs Client Library for Python Python samples

These early-stage libraries are also available:

Client library Documentation All client library samples
Google APIs Client Library for Go (alpha) google-api-go-client/ Go samples
Google API Libraries for Google Web Toolkit (alpha) gwt-google-apis/ GWT samples
Google APIs Client Library for Node.js (alpha) google-api-nodejs-client/ Node.js samples
Google APIs Client Library for Ruby (alpha) Google APIs Client Library for Ruby Ruby samples

In addition to the Google-supported client libraries listed in the table above, a set of third-party Dart libraries (with samples) is available for the Dart language.

Prerequisites

The REST API uses OAuth as the authorization mechanism. When you configure your pull queue, make sure that your queue.yaml file supplies the email addresses of the users that can access the queue using the REST API. The OAuth scope for all methods is https://www.googleapis.com/auth/taskqueue.

Using the task queue REST API with the Python Google API library

This section demonstrates the use of the REST API in an application called gtaskqueue, which is shipped with the samples (see above). Installing this application creates two Python binaries in /usr/local/bin: gtaskqueue and gtaskqueue_puller. The gtaskqueue binary allows you to interact with the REST API via the command line. The gtaskqueue_puller binary is a command-line tool that can continually grab tasks from a pull queue, and execute an arbitrary binary for each task that is pulled. It also supports sending the output of the binary to an arbitrary URL.

The gtaskqueue tool uses the Google APIs Client Library for Python to interact with the REST API. The command-line functions are based on the gflags Python library. The sections below show the Python code used to import the library and use it to lease and delete tasks. The final section describes how to implement scaling in your application.

Importing the Client Library for Python

To begin using the library, you need to install it in your local environment. After installation, you can import the appropriate client libraries and build the taskqueue service:

from apiclient.discovery import build

task_api = build('taskqueue', 'v1beta2')

Once you've built the task queue service, your application can access methods from the library allowing you to interact with the REST API. The following sections describe the two most common functions used with the Task Queue API, allowing you to lease and delete tasks.

Leasing Tasks

The Google APIs Client Library provides methods that invoke the Tasks.lease method in the REST API. When you create a lease, you need to specify the number of tasks to lease (up to a maximum of 1,000 tasks); the API returns the specified number of in order of the oldest task ETA.

You also need to specify the duration of the lease in seconds (up to a maximum of one week). The lease must be long enough to enable you to finish all the leased tasks, yet short enough that if your consumer crashes, the tasks will be available for lease by other clients relatively soon. Similarly, if you lease too many tasks at once and your client crashes, a large number of tasks will become unavailable until the lease expires.

You can specify a deadline, the amount of time to wait before aborting the lease_tasks() method call.

The following code from the gtaskqueue sample shows how to lease tasks using the library.

def _get_tasks_from_queue(self):

  """Gets the available tasks from the taskqueue.

  Returns:
    Lease response object.
  """
  try:
    tasks_to_fetch = self._num_tasks_to_lease()
    lease_req = self.task_api.tasks().lease(project=FLAGS.project_name,
                                            taskqueue=FLAGS.taskqueue_name,
                                            leaseSecs=FLAGS.lease_secs,
                                            numTasks=tasks_to_fetch,
                                            body={})
    result = lease_req.execute()
    return result
  except HttpError, http_error:
    logger.error('Error during lease request: %s' % str(http_error))
    return None

This code enables a command-line tool for leasing a specified number of tasks for a set duration:

gtaskqueue leasetask --project="gpullqueue1" \
--taskqueue_name=appengtaskpuller \
--lease_secs=30 \
--num_tasks=100

When run, this command-line tool constructs the following URI call to the REST API:

POST
https://www.googleapis.com/taskqueue/v1beta2/projects/gpullqueue1/taskqueues/appengtaskpuller/tasks/lease?alt=json&lease_secs=30&numTasks=100

This request returns an array of 100 tasks with the following JSON structure:

{
  "kind": "taskqueues#tasks",
  "items": [
    {
      "kind": "taskqueues#task",
      "id": string,
      "queueName": string,
      "payloadBase64": string,
      "enqueueTimestamp": number,
      "leaseTimestamp": number
    }
    ...
  ]
}

After processing each task, you need to delete it, as described in the following section.

Deleting Tasks

In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.

You can delete an individual task or a list of tasks using the REST method Tasks.delete. You must know the name of a task in order to delete it. You can get the task name from the id field of the Task object returned by Tasks.lease.

Call delete if you have finished a task, even if you have exceeded the lease time. Tasks should be idempotent, so even if a task lease expires and another client leases the task, performing the same task twice should not cause an error.

Returning to our gtaskqueue example, the following code snippet uses the Google APIs Client Library for Python to delete tasks from a queue:

def _delete_task_from_queue(self, task_api):
  try:
    delete_request = task_api.tasks().delete(project=FLAGS.project_name,
                                             taskqueue=FLAGS.taskqueue_name,
                                             task=self.task_id)
    delete_request.execute()
  except HttpError, http_error:
    logger.error('Error deleting task %s from taskqueue. Error details %s'
                 %(self.task_id, str(http_error)))

This code enables a command for naming a task to delete:

gtaskqueue deletetask --project_name="gpullqueue1" \
--taskqueue_name=appengtaskpuller \
--task_name=taskID

When run, this command constructs the following URI call to the REST API:

DELETE
https://www.googleapis.com/taskqueue/v1beta2/projects/gpullqueue1/taskqueues/appengtaskpuller/tasks/taskID

If the delete command is successful, the API returns an HTTP 200 response. If deletion fails, the API returns an HTTP failure code.

Quotas and limits for pull queues

Enqueuing a task counts counts toward the following quotas:

  • Task Queue Stored Task Count
  • Task Queue API Calls
  • Task Queue Stored Task Bytes

Leasing a task counts toward the following quotas:

  • Task Queue API Calls
  • Outgoing Bandwidth (if using the REST API)

The Task Queue Stored Task Bytes quota is configurable in queue.yaml by setting total_storage_limit. This quota counts towards your Stored Data (billable) quota.

The following limits apply to the use of pull queues:

Pull Queue Limits
Maximum task size1MB
Maximum number of active queues (not including the default queue)Free apps: 10 queues, Billed apps: 100 queues
Maximum countdown/ETA for a task 30 days from the current date and time
Maximum number of tasks that can be added in a batch 100 tasks
Maximum number of tasks that can be added in a transaction 5 tasks
Maximum number of tasks that you can lease in a single operation 1000 tasks
Maximum payload size when leasing a batch of tasks32MB (1MB when using the REST API)

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.