Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
設定檔
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
服務的每個版本是由 .yaml
檔案來定義,該檔案會說明服務與版本的名稱。YAML 檔案通常與其定義的服務同名,但這並非必要條件。如要部署某項服務的數個版本,可以在同一個目錄中建立多個 yaml 檔案 (每個版本各一個)。
一般來說,您可以為各項服務建立目錄,當中包含該項服務的 YAML 檔案和相關聯原始碼。最上層應用程式目錄中會包含選用的應用程式層級設定檔 (dispatch.yaml
、cron.yaml
、index.yaml
和 queue.yaml
)。以下範例呈現了三項服務。service1
和 service2
中的原始檔與 YAML 檔案位於相同層級,service3
中則包含兩個版本的 YAML 檔案。

如為小型、簡易型專案,您可以將所有應用程式檔案存放於同一個目錄:

每一個 YAML 檔案都必須含有一個版本參數。如要定義預設服務,您可以明確納入 service: default
參數,或是將服務參數排除在檔案之外。
每個服務設定檔都會定義特定服務/版本的資源調度類型和執行個體類別。視指定的資源調度類型而定,您必須使用不同的資源調度參數。如果您未指定資源調度設定,預設值為自動調整資源配置。如需資源調度和執行個體類別設定的相關說明,請參閱 app.yaml
參考資料部分。
您也可以針對各項服務指定設定,將網址要求對應至特定指令碼及識別靜態檔案,藉此提升伺服器的工作效率。這些設定也會包含在 yaml 檔案中,相關說明請見 app.yaml
參考資料部分。
預設服務
每個應用程式均有一項預設服務。您可以在 app.yaml
中使用 service: default
設定來定義預設服務,不過此為選用動作。凡是與服務有關的設定參數均可套用至預設服務。
選用設定檔
這些設定檔控制適用於應用程式中所有服務的選用功能:
如要在 App Engine 中部署這些設定檔的更新內容,請在設定檔所在的目錄中執行以下指令:
gcloud app deploy [CONFIG_FILE]
範例
以下舉例說明如何設定應用程式的 YAML 檔案,這個應用程式包含三項服務:一項是處理網路要求的預設服務,另外兩項則是處理行動要求和後端處理的服務。
首先,請定義名為 app.yaml
的設定檔,用於處理所有與網頁相關的要求:
runtime: python27
api_version: 1
threadsafe: true
如果此應用程式的 Google Cloud 控制台專案 ID 為 simple-sample
,則這項設定會建立預設服務,並自動調整規模,公開地址為 https://simple-sample.uc.r.appspot.com
。
接下來,假設您要建立服務來處理行動網頁要求。為了方便行動使用者 (在本例中),最大待處理延遲時間將只會是一秒,而且我們會隨時保留至少兩個閒置的例項。如要設定這項功能,請建立 mobile-frontend.yaml
設定檔,並加入下列內容:
service: mobile-frontend
runtime: python27
api_version: 1
threadsafe: true
automatic_scaling:
min_idle_instances: 2
max_pending_latency: 1s
這時,您就能在 https://mobile-frontend-dot-simple-sample.uc.r.appspot.com
中存取這個檔案建立的服務。
最後,新增名為 my-service
的服務,用於處理靜態後端工作。這可能是從 Datastore 匯出資料至 BigQuery 的持續性工作。其工作量相對固定,因此,無論在任何指定時間,您只需要一項常駐服務。此外,這些工作需要處理大量記憶體內處理作業,因此您需要使用記憶體配置較多的服務。如要設定這項功能,請建立 my-service.yaml
設定檔,並加入下列內容。
service: my-service
runtime: python27
api_version: 1
threadsafe: true
instance_class: B8
manual_scaling:
instances: 1
這時,您就能在 https://my-service-dot-simple-sample.uc.r.appspot.com
中存取這個檔案建立的服務。
請注意 manual_scaling:
設定。instances:
參數可讓 App Engine 知道需要為這項服務建立幾個執行個體。
另外建議您下載這個
Python 示範應用程式,好好研究一下。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","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-09-04 (世界標準時間)。"],[[["\u003cp\u003eEach service version is defined in a \u003ccode\u003e.yaml\u003c/code\u003e file, specifying the service's name and version, with multiple versions of a service having separate YAML files.\u003c/p\u003e\n"],["\u003cp\u003eServices typically reside in their own directories containing their YAML files and source code, while optional application-level configuration files like \u003ccode\u003edispatch.yaml\u003c/code\u003e are placed in the top-level app directory.\u003c/p\u003e\n"],["\u003cp\u003eEach YAML file must contain a version parameter, and the default service can be designated explicitly with \u003ccode\u003eservice: default\u003c/code\u003e or implicitly by omitting the service parameter.\u003c/p\u003e\n"],["\u003cp\u003eYAML files configure each service's scaling type and instance class, with automatic scaling as the default if no scaling is specified, and they can also define URL-to-script mappings and identify static files.\u003c/p\u003e\n"],["\u003cp\u003eOptional configuration files such as \u003ccode\u003edispatch.yaml\u003c/code\u003e, \u003ccode\u003equeue.yaml\u003c/code\u003e, \u003ccode\u003eindex.yaml\u003c/code\u003e, and \u003ccode\u003ecron.yaml\u003c/code\u003e allow for the customization of routing, queues, indexes, and scheduled tasks across all services.\u003c/p\u003e\n"]]],[],null,["# Configuration Files\n\nEach version of a service is defined in a `.yaml` file, which gives the name of\nthe service and version. The YAML file usually takes the same name as the\nservice it defines, but this is not required. If you are deploying several\nversions of a service, you can create multiple yaml files in the same directory,\none for each version.\n\nTypically, you create a directory for each service, which contains the service's\nYAML files and associated source code. Optional application-level configuration\nfiles (`dispatch.yaml`, `cron.yaml`, `index.yaml`, and `queue.yaml`) are\nincluded in the top level app directory. The example below shows three services.\nIn `service1` and `service2`, the source files are at the same level as the YAML\nfile. In `service3`, there are YAML files for two versions.\n\nFor small, simple projects, all the app's files can live in one directory:\n\nEvery YAML file must include a version parameter. To define the default service,\nyou can explicitly include the parameter `service: default` or leave the service\nparameter out of the file.\n\nEach service's configuration file defines the scaling type and instance class\nfor a specific service/version. Different scaling parameters are used depending\non which type of scaling you specify. If you do not specify scaling, automatic\nscaling is the default. The scaling and instance class settings are described in\nthe [`app.yaml`\nreference](/appengine/docs/legacy/standard/python/config/appref#scaling_elements)\nsection.\n\nFor each service you can also specify settings that map URL requests to specific\nscripts and identify static files for better server efficiency. These settings\nare also included in the yaml file and are described in the\n[`app.yaml` reference](/appengine/docs/legacy/standard/python/config/appref)\nsection.\n\n### The default service\n\nEvery application has a single default service. You can define the default\nservice in the `app.yaml` with the setting\n`service: default`, but it isn't necessary to do this. All configuration\nparameters relevant to services can apply to the default service.\n\n### Optional configuration files\n\nThese configuration files control optional features that apply to all the\nservices in an app:\n\n- [`dispatch.yaml`](/appengine/docs/legacy/standard/python/reference/dispatch-yaml) overrides routing default rules by sending incoming requests to a specific service based on the path or hostname in the URL.\n- [`queue.yaml`](/appengine/docs/legacy/standard/python/config/queueref) configures both push queues and pull queues.\n- [`index.yaml`](/appengine/docs/legacy/standard/python/configuring-datastore-indexes-with-index-yaml) specifies which indexes your app needs if using Datastore queries.\n- [`cron.yaml`](/appengine/docs/legacy/standard/python/config/cronref) configures regularly scheduled tasks that operate at defined times or regular intervals.\n\nTo deploy updates of these configuration files to App Engine, run the\nfollowing command from the directory where they are located: \n\n gcloud app deploy [CONFIG_FILE]\n\n### An example\n\nHere is an example of how you would configure YAML files for an application that\nhas three services: a default service that handles web requests, plus two more\nservices that handle mobile requests and backend processing.\n\nStart by defining a configuration file named `app.yaml` that will handle all\nweb-related requests: \n\n runtime: python27\n api_version: 1\n threadsafe: true\n\nIf the Google Cloud console project ID for this app is `simple-sample` then\nthis configuration would create a default service with automatic scaling and a\npublic address of `https://simple-sample.uc.r.appspot.com`.\n\nNext, assume that you want to create a service to handle mobile web requests.\nFor the sake of the mobile users (in this example) the max pending latency will\nbe just a second and we'll always have at least two instances idle. To configure\nthis you would create a `mobile-frontend.yaml` configuration file. with the\nfollowing contents: \n\n service: mobile-frontend\n runtime: python27\n api_version: 1\n threadsafe: true\n\n automatic_scaling:\n min_idle_instances: 2\n max_pending_latency: 1s\n\nThe service this file creates would then be reachable at `https://mobile-frontend-dot-simple-sample.uc.r.appspot.com`.\n\nFinally, add a service, called `my-service` for handling static backend work.\nThis could be a continuous job that exports data from Datastore to BigQuery. The\namount of work is relatively fixed, therefore you simply need 1 resident service\nat any given time. Also, these jobs will need to handle a large amount of\nin-memory processing, thus you'll want services with an increased memory\nconfiguration. To configure this you would create a `my-service.yaml`\nconfiguration file with the following contents. \n\n service: my-service\n runtime: python27\n api_version: 1\n threadsafe: true\n\n instance_class: B8\n manual_scaling:\n instances: 1\n\nThe service this file creates would then be reachable at\n`https://my-service-dot-simple-sample.uc.r.appspot.com`.\n\nNotice the `manual_scaling:` setting. The `instances:` parameter tells App\nEngine how many instances to create for this service.\nYou might also want to download this [Python demo app](https://github.com/GoogleCloudPlatform/appengine-modules-helloworld-python) and take a look."]]