Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Sobald Aufgaben sich in einer Pull-Warteschlange befinden, können sie von einem Worker freigegeben werden. Nachdem die Aufgaben verarbeitet wurden, müssen sie vom Worker gelöscht werden.
Diese Methode gilt nur für Worker, die in einem Dienst in der Standardumgebung ausgeführt werden.
Wenn Sie Pull-Warteschlangen verwenden, müssen Sie die Worker je nach Verarbeitungsvolumen skalieren.
Aufgaben leasen
Wenn sich die Aufgaben in der Warteschlange befinden, können eine oder mehrere Aufgaben mithilfe der Methode taskqueue.Lease von einem Worker freigegeben werden. Es kann zu einer kurzen Verzögerung kommen, bis Aufgaben, die erst kürzlich mit taskqueue.Add hinzugefügt wurden, über taskqueue.Lease verfügbar werden.
Sie geben die Anzahl der freizugebenden Aufgaben (bis zu 1.000 Aufgaben) und die Freigabedauer in Sekunden (bis zu einer Woche) an, um die Freigabe anzufordern. Die Freigabedauer muss so lang sein, dass auch die langsamste Aufgabe vor Ablauf des Freigabezeitraums abgeschlossen werden kann. Sie können eine Aufgabenfreigabe mit taskqueue.ModifyLease bearbeiten.
Sobald eine Aufgabe freigegeben wurde, ist sie nicht mehr für die Verarbeitung durch einen anderen Worker verfügbar und steht erst dann wieder zur Verfügung, wenn die Freigabe abgelaufen ist.
Im folgenden Beispielcode werden 100 Aufgaben aus der Warteschlange pull-queue für eine Stunde freigegeben:
Nicht alle Aufgaben sind identisch. Sie können Aufgaben im Code mit Tags kennzeichnen und Aufgaben dann nach Tags freigeben. Das Tag dient dann als Filter. Im folgenden Codebeispiel wird gezeigt, wie Sie Aufgaben kennzeichnen und dann nach Tags freigeben:
_,err=taskqueue.Add(ctx,&taskqueue.Task{Payload:[]byte("parse"),Method:"PULL",Tag:"parse",},"pull-queue")_,err=taskqueue.Add(ctx,&taskqueue.Task{Payload:[]byte("render"),Method:"PULL",Tag:"render",},"pull-queue")// leases render tasks, but not parsetasks,err=taskqueue.LeaseByTag(ctx,100,"pull-queue",3600,"render")// Leases up to 100 tasks that have same tag.// Tag is that of "oldest" task by ETA.tasks,err=taskqueue.LeaseByTag(ctx,100,"pull-queue",3600,"")
Abfrageraten regeln
Worker, die die Warteschlange nach Aufgaben zum Leasen abfragen, sollten erkennen, ob sie Aufgaben schneller freizugeben versuchen, als die Warteschlange sie bereitstellen kann. Wenn dieser Fehler auftritt, gibt taskqueue.Lease einen Backoff-Fehler zurück.
Ihr Code muss diese Fehler beheben, den Aufruf von taskqueue.Lease beenden und es dann später noch einmal versuchen. Wenn Sie dieses Problem vermeiden möchten, legen Sie beim Aufrufen von taskqueue.Lease ein höheres RPC-Zeitlimit fest. Stellen Sie die Aufrufe auch dann zurück, wenn bei einer Lease-Anfrage eine leere Aufgabenliste geliefert wird.
Wenn Sie mehr als 10 LeaseTasks-Anfragen pro Warteschlange und Sekunde generieren, liefern nur die ersten 10 Anfragen Ergebnisse. Wenn Anfragen diesen Grenzwert überschreiten, wird OK mit null Ergebnissen geliefert.
Aufgaben in der Google Cloud -Console überwachen
So rufen Sie Informationen zu allen Aufgaben und Warteschlangen in der Anwendung auf:
Öffnen Sie in der Google Cloud Console die Seite Cloud Tasks und suchen Sie in der Spalte Typ nach dem Wert Pull.
Klicken Sie auf den Namen der gewünschten Warteschlange, um die zugehörige Detailseite zu öffnen. Hier werden alle Aufgaben angezeigt, die sich in der ausgewählten Warteschlange befinden.
Aufgaben löschen
Nachdem ein Worker eine Aufgabe durchgeführt hat, muss er die Aufgabe aus der Warteschlange löschen. Wenn Aufgaben in einer Warteschlange verbleiben, nachdem die Verarbeitung durch einen Worker abgeschlossen ist, ist der Worker-Vorgang wahrscheinlich fehlgeschlagen. In diesem Fall werden die Aufgaben von einem anderen Worker verarbeitet.
Sie können eine Aufgabenliste wie die von taskqueue.Lease zurückgegebene löschen. Übergeben Sie sie dazu an taskqueue.DeleteMulti:
tasks,err=taskqueue.Lease(ctx,100,"pull-queue",3600)// Perform some work with the tasks heretaskqueue.DeleteMulti(ctx,tasks,"pull-queue")
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eWorkers can lease tasks from a pull queue, which temporarily makes them unavailable to other workers for processing, until the lease expires.\u003c/p\u003e\n"],["\u003cp\u003eAfter processing, the worker must delete the task from the queue to ensure it's not processed again.\u003c/p\u003e\n"],["\u003cp\u003eTasks can be tagged, allowing workers to lease specific tasks based on their tag using the \u003ccode\u003eLeaseByTag\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eWhen polling for tasks, workers should handle back-off errors returned from \u003ccode\u003etaskqueue.Lease\u003c/code\u003e and avoid exceeding 10 LeaseTasks requests per queue per second, to prevent issues with task availability.\u003c/p\u003e\n"],["\u003cp\u003eThe task queue API discussed is supported for first-generation runtimes, and the page provides information on upgrading to corresponding second-generation runtimes.\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| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-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/go111/taskqueue/pull/creating-pull-queues).\n- [Create tasks](/appengine/docs/legacy/standard/go111/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 [`taskqueue.Lease`](/appengine/docs/legacy/standard/go111/taskqueue/reference#Lease) method. There may be a short delay before tasks recently added using [`taskqueue.Add`](/appengine/docs/legacy/standard/go111/taskqueue/reference#Add) become available via [`taskqueue.Lease`](/appengine/docs/legacy/standard/go111/taskqueue/reference#Lease).\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 [`taskqueue.ModifyLease`](/appengine/docs/legacy/standard/go111/taskqueue/reference#ModifyLease).\n\nLeasing a task makes it unavailable for processing by another worker, and it remains unavailable until the lease expires.\n| **Note:** `taskqueue.Lease` operates only on pull queues. If you attempt to lease tasks added in a push queue, App Engine returns an error.\n\nThe following code sample leases 100 tasks from the queue `pull-queue` for one hour: \n\n tasks, err := taskqueue.Lease(ctx, 100, \"pull-queue\", 3600)\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. The following code sample demonstrates how to tag tasks and then lease by tags: \n\n _, err = taskqueue.Add(ctx, &taskqueue.Task{\n \tPayload: []byte(\"parse\"), Method: \"PULL\", Tag: \"parse\",\n }, \"pull-queue\")\n _, err = taskqueue.Add(ctx, &taskqueue.Task{\n \tPayload: []byte(\"render\"), Method: \"PULL\", Tag: \"render\",\n }, \"pull-queue\")\n\n // leases render tasks, but not parse\n tasks, err = taskqueue.LeaseByTag(ctx, 100, \"pull-queue\", 3600, \"render\")\n\n // Leases up to 100 tasks that have same tag.\n // Tag is that of \"oldest\" task by ETA.\n tasks, err = taskqueue.LeaseByTag(ctx, 100, \"pull-queue\", 3600, \"\")\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, a back-off error will be returned from `taskqueue.Lease`. \n\nYour code must handle these errors, back off from calling `taskqueue.Lease`, and then try again later. To avoid this problem, consider setting a higher RPC deadline when calling `taskqueue.Lease`. You should also back off when a lease request returns an empty list of tasks.\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 `taskqueue.Lease`, by passing it to [`taskqueue.DeleteMulti`](/appengine/docs/legacy/standard/go111/taskqueue/reference#DeleteMulti): \n\n tasks, err = taskqueue.Lease(ctx, 100, \"pull-queue\", 3600)\n // Perform some work with the tasks here\n\n taskqueue.DeleteMulti(ctx, tasks, \"pull-queue\")"]]