A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/appengine/docs/legacy/standard/python/taskqueue/pull/leasing-pull-tasks below:

Leasing Pull Tasks | App Engine standard environment for Python 2

Leasing Pull Tasks

Stay organized with collections Save and categorize content based on your preferences.

Once tasks are in a pull queue, a worker can lease them. After the tasks are processed the worker must delete them.

This API is supported for first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes. If you are updating to the App Engine Python 3 runtime, refer to the migration guide to learn about your migration options for legacy bundled services. Before you begin Important context Leasing tasks

After the tasks are in the queue, a worker can lease one or more of them using the lease_tasks() method. 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 modify 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.

The lease_tasks() method returns a Task object containing a list of tasks leased from the queue.

Note: lease_tasks() operates only on pull queues. If you attempt to lease tasks added in a push queue, App Engine throws an exception.

The following code sample leases 100 tasks from the queue

pull-queue

for one hour:

from google.appengine.api import taskqueue

q = taskqueue.Queue('pull-queue')
q.lease_tasks(3600, 100)
Batching with task tags

Beta

This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the Service Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see the launch stage descriptions.

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.
Regulating polling rates

Workers that poll the queue for tasks to lease should detect whether they are attempting to lease tasks faster than the queue can supply them. If this failure occurs, the following exceptions from lease_tasks() can be generated:


Your code must catch these exceptions, back off from calling lease_tasks(), and then try again later. To avoid this problem, consider setting a higher RPC deadline when calling lease_tasks(). You should also back off when a lease request returns an empty list of tasks.

If you generate more than 10 LeaseTasks requests per queue per second, only the first 10 requests will return results. If requests exceed this limit, OK is returned with zero results.

Monitoring tasks in the Google Cloud console

To view information about all the tasks and queues in your application:

  1. Open the Cloud Tasks page in the Google Cloud console and look for the Pull value in column Type.

    Go to Cloud Tasks

  2. Click on the name of the queue in which you are interested, opening the queue details page. It displays all of the tasks in the selected queue.

Deleting tasks

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 will 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)
An end-to-end example of pull queues

For a simple but complete end-to-end example of using pull queues in Python, see appengine-pullqueue-counter.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-07 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["Workers can lease tasks from a pull queue using the `lease_tasks()` method, making them unavailable to other workers until the lease expires, with a maximum of 1,000 tasks and a one-week lease duration."],["Tasks can be tagged, allowing workers to lease tasks by specific tags using the `lease_tasks_by_tag()` method, enabling the filtering of tasks."],["Workers must delete tasks from the queue after processing them using `delete_tasks()`, to ensure that tasks that have been processed are not considered further for work."],["Workers polling the queue must handle `TransientError` or `DeadlineExceededError` exceptions and implement a back-off strategy when these occur or when lease requests return an empty list of tasks, this applies also when there are more than 10 LeaseTasks requests per queue per second."],["The Cloud Tasks page in the Google Cloud console provides monitoring capabilities for all tasks and queues, allowing users to view details and status of the tasks."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4