fromgoogle.appengine.apiimporttaskqueueq=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 parseq.lease_tasks_by_tag(3600,100)# Leases up to 100 tasks that have same tag.
fromgoogle.appengine.apiimporttaskqueueq=taskqueue.Queue('pull-queue')tasks=q.lease_tasks(3600,100)# Perform some work with the tasks hereq.delete_tasks(tasks)
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eWorkers can lease tasks from a pull queue using the \u003ccode\u003elease_tasks()\u003c/code\u003e method, making them unavailable to other workers until the lease expires, with a maximum of 1,000 tasks and a one-week lease duration.\u003c/p\u003e\n"],["\u003cp\u003eTasks can be tagged, allowing workers to lease tasks by specific tags using the \u003ccode\u003elease_tasks_by_tag()\u003c/code\u003e method, enabling the filtering of tasks.\u003c/p\u003e\n"],["\u003cp\u003eWorkers must delete tasks from the queue after processing them using \u003ccode\u003edelete_tasks()\u003c/code\u003e, to ensure that tasks that have been processed are not considered further for work.\u003c/p\u003e\n"],["\u003cp\u003eWorkers polling the queue must handle \u003ccode\u003eTransientError\u003c/code\u003e or \u003ccode\u003eDeadlineExceededError\u003c/code\u003e 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.\u003c/p\u003e\n"],["\u003cp\u003eThe 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.\u003c/p\u003e\n"]]],[],null,["# Leasing Pull Tasks\n\nOnce tasks are in a pull queue, a worker can lease them. After the tasks are processed\nthe worker must delete them.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| python3\n|\n| /services/access). If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nBefore you begin\n----------------\n\n- Create a [pull queue](/appengine/docs/legacy/standard/python/taskqueue/pull/creating-pull-queues).\n- [Create tasks](/appengine/docs/legacy/standard/python/taskqueue/pull/creating-tasks) and add them to the pull queue.\n\nImportant context\n-----------------\n\n- This method is only applicable to workers that are running within a service in the standard environment.\n- When you use pull queues, you are responsible for scaling your workers based on your processing volume.\n\nLeasing tasks\n-------------\n\nAfter the tasks are in the queue, a worker can lease one or more of them using the [`lease_tasks()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Queue.lease_tasks) method. There may be a short delay before tasks recently added using [`add()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Queue.add) become available via [`lease_tasks()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Queue.lease_tasks).\n\nWhen 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()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Queue.modify_task_lease).\n\nLeasing a task makes it unavailable for processing by another worker, and it remains unavailable until the lease expires.\n\nThe [`lease_tasks()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Queue.lease_tasks) method returns a [Task](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue#google.appengine.api.taskqueue.Task) object containing a list of tasks leased from the queue.\n| **Note:** `lease_tasks()` operates only on pull queues. If you attempt to lease tasks added in a push queue, App Engine throws an exception.\nThe following code sample leases 100 tasks from the queue `pull-queue` for one hour:\n\n\u003cbr /\u003e\n\n from google.appengine.api import taskqueue\n\n q = taskqueue.Queue('pull-queue')\n q.lease_tasks(3600, 100)\n\n### Batching with task tags\n\n|\n| **Beta**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nNot all tasks are alike; your code can \"tag\" tasks and then choose tasks to lease by tag. The tag acts as a filter. \n\n from google.appengine.api import taskqueue\n\n q = taskqueue.Queue('pull-queue')\n q.add(taskqueue.Task(payload='parse1', method='PULL', tag='parse'))\n q.add(taskqueue.Task(payload='parse2', method='PULL', tag='parse'))\n q.add(taskqueue.Task(payload='render1', method='PULL', tag='render'))\n q.add(taskqueue.Task(payload='render2', method='PULL', tag='render'))\n\n q.lease_tasks_by_tag(3600, 100, 'render') # leases render tasks, but not parse\n\n q.lease_tasks_by_tag(3600, 100) # Leases up to 100 tasks that have same tag.\n\n### Regulating polling rates\n\nWorkers 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:\n\n- `google.appengine.api.taskqueue.TransientError`\n- `google.appengine.runtime.apiproxy_errors.DeadlineExceededError`\n\n\u003cbr /\u003e\n\nYour 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()](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue.taskqueue#google.appengine.api.taskqueue.taskqueue.Queue.lease_tasks). You should also back off when a lease request returns an empty list of tasks. \n\nIf 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.\n\n\nMonitoring tasks in the Google Cloud console\n--------------------------------------------\n\nTo view information about all the tasks and queues in your application:\n\n1. Open the **Cloud Tasks** page in the Google Cloud console and look for the *Pull* value in column **Type**.\n\n [Go to Cloud Tasks](https://console.cloud.google.com/cloudtasks)\n2. 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.\n\nDeleting tasks\n--------------\n\nOnce 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.\n\nYou can delete a list of tasks, such as that returned by `lease_task()`, simply by passing it to [`delete_tasks()`](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.taskqueue.taskqueue#google.appengine.api.taskqueue.taskqueue.Queue.delete_tasks): \n\n from google.appengine.api import taskqueue\n\n q = taskqueue.Queue('pull-queue')\n tasks = q.lease_tasks(3600, 100)\n # Perform some work with the tasks here\n q.delete_tasks(tasks)\n\nAn end-to-end example of pull queues\n------------------------------------\n\nFor a simple but complete end-to-end example of using pull queues in Python, see\n[appengine-pullqueue-counter](https://github.com/GoogleCloudPlatform/appengine-pullqueue-counter)."]]