[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-19 (世界標準時間)。"],[[["\u003cp\u003eThis document details how to create and enqueue tasks within push queues, using \u003ccode\u003ePushTask\u003c/code\u003e and \u003ccode\u003ePushQueue\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eTasks can be assigned to specific worker services and handlers via a \u003ccode\u003etarget\u003c/code\u003e (service) and \u003ccode\u003eurl\u003c/code\u003e (handler), with options to set the target when constructing the task or within the queue's configuration.\u003c/p\u003e\n"],["\u003cp\u003eData can be passed to handlers as query parameters for \u003ccode\u003eGET\u003c/code\u003e or \u003ccode\u003ePULL\u003c/code\u003e methods, or within the HTTP request payload for \u003ccode\u003ePOST\u003c/code\u003e or \u003ccode\u003ePUT\u003c/code\u003e methods, using the \u003ccode\u003ePushTask\u003c/code\u003e constructor.\u003c/p\u003e\n"],["\u003cp\u003eWhile App Engine generates unique task names, you can assign custom names for de-duplication, ensuring a task is only added once within a 9-day window, although this carries a performance overhead.\u003c/p\u003e\n"],["\u003cp\u003eThis API supports first-generation runtimes and can be used for updating to second-generation runtimes, with a migration guide available for the PHP 7/8 runtime.\u003c/p\u003e\n"]]],[],null,["# Creating Tasks\n\nThis page describes how to create tasks and place them in push queues. When you\nwant to process a task, you must create a new task object and place it on a\nqueue. You can explicitly specify the service and handler that process the task,\nand optionally pass task-specific data along to the handler. You can also\nfine-tune the configuration for the task, like scheduling a time in the future\nwhen it should be executed or limiting the number of times you want the task to\nbe retried if it fails.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| php-gen2\n|\n| /services/access). If you are updating to the App Engine PHP 7/8 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/php-differences) to learn about your migration options for legacy bundled services.\n\nCreating a new task\n-------------------\n\nTo create and enqueue a task, create a [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask)\nobject and call its `add()` method. You can add to a queue specified in\n`queue.yaml` by supplying a queue name argument to `add()`. Alternatively,\ncalling `add()` with no arguments will add the task to the default queue.\n\nYou can also add tasks in bulk to a queue using [PushQueue](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushQueue). In the\nfollowing example, two [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask) objects are added to a\n[PushQueue](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushQueue) using the `addTasks()` method.\n\nThe following code sample shows how to add a single task: \n\n $task = new PushTask(\n '/worker',\n ['name' =\u003e 'john doe', 'action' =\u003e 'send_reminder']);\n $task_name = $task-\u003eadd();\n\nThe following code sample shows how to add multiple tasks at once: \n\n $task1 = new PushTask('/someUrl');\n $task2 = new PushTask('/someOtherUrl');\n $queue = new PushQueue();\n $queue-\u003eaddTasks([$task1, $task2]);\n\nWhen you use `PushTask` and `PushQueue`, include these statements at the top of\nyour PHP file: \n\n use google\\appengine\\api\\taskqueue\\PushTask;\n use google\\appengine\\api\\taskqueue\\PushQueue;\n\nSpecifying the worker service\n-----------------------------\n\nWhen a task is popped off its queue, the Task Queue service sends it on\nto a worker service. Every task has a *target* and a *url*, which determine\nwhat service and handler will ultimately perform the task.\n\n### `target`\n\nThe target specifies the service that will receive the HTTP request to\nperform the task. It is a string that specifies a service/version/instance in\nany one of the [canonical forms](/appengine/docs/legacy/standard/php/how-requests-are-routed#routing_via_url). The most often-used forms are: \n\n service\n version.service\n instance.version.service\n\nThe target string is prepended to the domain name of your app. There are three\nways to set the target for a task:\n\n- Declare the target when you construct the task.\n\n\n You can set the target explicitly when creating the task by using the header\n parameter in the `$options`\n array when you construct the [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask#__construct)\n object:\n\n $task = new PushTask(\n '/worker',\n [],\n ['header' =\u003e \"Host: versionHostname\"]);\n\n \u003cbr /\u003e\n\n- Include a `target` directive when you define a queue in the\n `queue.yaml`, as in the [definition](/appengine/docs/legacy/standard/php/taskqueue/push/creating-push-queues) of `queue-blue`.\n All tasks added to a queue with a `target` will use that target, even if\n a different target was assigned to the task at construction time.\n\n- If no target is specified according to either of the previous two methods,\n then the task's target is the version of the service that enqueues it.\n Note that if you enqueue a task from the default service and version in\n this manner, and the default version changes before the task executes, it\n will run in the new default version.\n\n### `url`\n\nThe `url` selects one of the handlers in the target service, which will\nperform the task.\n\nThe `url` should match one of the handler URL patterns in the target\nservice. The `url` can include query parameters if the method specified in the task is\n`GET` or `PULL`. If no `url` is specified the default URL\n`/_ah/queue/[QUEUE_NAME]` is used, where `[QUEUE_NAME]` is the name of\nthe task's queue.\n\nPassing data to the handler\n---------------------------\n\nYou can pass data to the handler as query parameters in the task's URL, but\nonly if the method specified in the task is `GET` or `PULL`.\n\nThe [`PushTask`](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask) constructor has a positional argument for query_data.\nThe data is usually a dictionary of key-value pairs. If the task's method is\n`POST` or `PUT`, the data is added to the payload of the HTTP request. If the\nmethod is GET it is added to the URL as query parameters.\n\nNaming a task\n-------------\n\nWhen you create a new task, App Engine assigns the task a unique name by\ndefault. However, you can assign your own name to a task by using the `name`\nparameter. An advantage of assigning your own task names is that named tasks are\nde-duplicated, which means you can use task names to\nguarantee\nthat a task is only added once. De-duplication continues for 9 days after the\ntask is completed or deleted.\n\nNote that de-duplication logic introduces significant performance overhead,\nresulting in increased latencies and potentially increased error rates\nassociated with named tasks. These costs can be magnified significantly if task\nnames are sequential, such as with timestamps. So, if you assign your own names,\nwe recommend using a well-distributed prefix for task names, such as a hash of\nthe contents.\n\nIf you assign your own names to tasks, note that the maximum name length is 500\ncharacters, and the name can contain uppercase and lowercase letters, numbers\nunderscores, and hyphens.\n\nWhat's next\n-----------\n\n- Learn how to [create task\n handlers](/appengine/docs/legacy/standard/php/taskqueue/push/creating-handlers)."]]