importpkg_resourcesfromgoogle.appengine.extimportvendor# Set PATH to your libraries folder.PATH='lib'# Add libraries installed in the PATH folder.vendor.add(PATH)# Add libraries to pkg_resources working set to find the distribution.pkg_resources.working_set.add_entry(PATH)
[[["容易理解","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\u003eThis guide explains the process of migrating pull queue functionality from Task Queues to Pub/Sub, which is the recommended method for pull queue work in App Engine.\u003c/p\u003e\n"],["\u003cp\u003eMigrate pull queues to Pub/Sub before push queues to avoid unexpected behavior with Cloud Tasks due to the \u003ccode\u003equeue.yaml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eMigrating to Pub/Sub may impact your app's pricing and quotas, as Pub/Sub has its own pricing structure and quotas that differ from those of Task Queues.\u003c/p\u003e\n"],["\u003cp\u003eBefore migration, you need to enable the Pub/Sub API, set up authentication for your app, and install the necessary Google Cloud CLI and Cloud Client Libraries.\u003c/p\u003e\n"],["\u003cp\u003eAfter migrating to Pub/Sub, you should delete your pull queues from Task Queues one by one using the \u003ccode\u003equeue.yaml\u003c/code\u003e file, and eventually remove the file entirely from your application.\u003c/p\u003e\n"]]],[],null,["# Migrating pull queues to Pub/Sub (Python)\n\nThis page explains how to migrate pull queue code from\nTask Queues to Pub/Sub. Pub/Sub is now the\npreferred way of performing pull queue work in App Engine.\n\nIf your app uses both pull queues and push queues, use this guide to migrate\nyour pull queues to Pub/Sub before migrating your push queues to\nthe new push queue service Cloud Tasks. Migrating your pull queues\nafter migrating push queues to Cloud Tasks is not recommended\nbecause the required use of the `queue.yaml` file is likely to cause unexpected\nbehavior with Cloud Tasks.\n\nFeatures not currently available in Pub/Sub\n-------------------------------------------\n\nThe following Task Queues features are not currently available\nin Pub/Sub:\n\n- Batching by tag\n- Automatic deduplication\n\nPricing and quotas\n------------------\n\nMigrating your pull queues to Pub/Sub might affect the pricing\nand quotas for your app.\n\n### Pricing\n\nPub/Sub has its own [pricing](/pubsub/pricing). As with\nTask Queues,\nsending requests to your App Engine app with Pub/Sub can\ncause your app to incur costs.\n\n### Quotas\n\nThe Pub/Sub [quotas](/pubsub/quotas) are different from the\nquotas for Task Queues. Like with Task Queues,\nsending requests to your App Engine app from Pub/Sub\nmight impact your App Engine\n[request quotas](/appengine/docs/standard/quotas#Requests).\n\nBefore migrating\n----------------\n\nIf you have not done so already, set up your [Python development environment](/python/docs/setup) to use a Python version that is compatible with Google Cloud, and install testing tools for creating isolated Python environments.\n\nThe following sections discuss the setup steps before migrating your pull queues\nto Pub/Sub.\n\n### Enabling the Pub/Sub API\n\nTo enable the Pub/Sub API, click [**Enable** on the\nPub/Sub API](https://console.cloud.google.com/apis/library/pubsub.googleapis.com)\nin the API Library. If you see a **Manage** button instead of an\n**Enable** button, you have previously enabled the Pub/Sub API for\nyour project and do not need to do so again.\n\n### Authenticating your app to the Pub/Sub API\n\nYou must authenticate your app to the Pub/Sub API. This section\ndiscusses authentication for two different use cases.\n\nTo develop or test your app locally, we recommend using a service account. For\ninstructions on setting up a service account and connecting it to your app, read\n[Obtaining and providing service account credentials manually](/docs/authentication#service-accounts).\n\nTo deploy your app on App Engine, you do not need to provide any new\nauthentication. The Application Default Credentials (ADC) infer authentication\ndetails for App Engine apps.\n\n### Downloading the Google Cloud CLI\n\nDownload and install the\n[Google Cloud CLI](/sdk/docs/install) to use the\ngcloud CLI with the Pub/Sub API if you have not\ninstalled it previously. Run the following command from your terminal if you\nalready have the Google Cloud CLI installed.\n\n\u003cbr /\u003e\n\n```bash\ngcloud components update\n```\n\n\u003cbr /\u003e\n\n### Importing the Cloud Client Libraries\n\nFollow the steps below to use the Pub/Sub\n\nPython client library\n\n\nwith your existing App Engine app:\n\n1. Update the `app.yaml` file. Follow the instructions for your version of\n Python:\n\n ### Python 2\n\n For Python 2 apps, add the latest versions of the `grpcio` library.\n\n The following is an example `app.yaml` file: \n\n runtime: python27\n threadsafe: yes\n api_version: 1\n\n libraries:\n - name: grpcio\n version: latest\n\n ### Python 3\n\n For Python 3 apps, specify the `runtime` element in your `app.yaml` file\n with a supported Python 3 version. For example: \n\n runtime: python310 # or another support version\n\n The Python 3 runtime installs libraries automatically, so you do not\n need to specify built-in libraries from the previous Python 2 runtime.\n If your Python 3 app is using other legacy bundled services when\n migrating, you can continue to specify the necessary built-in libraries.\n Otherwise, you can delete the unnecessary lines in your `app.yaml` file.\n2. Update the `requirements.txt` file. Follow the instructions for your version\n of Python:\n\n ### Python 2\n\n Add the Cloud Client Libraries for Pub/Sub to your\n list of dependencies in the `requirements.txt` file. \n\n google-cloud-pubsub\n\n Then run `pip install -t lib -r requirements.txt` to update the list of\n available libraries for your app.\n\n ### Python 3\n\n Add the Cloud Client Libraries for Pub/Sub to your\n list of dependencies in the `requirements.txt` file. \n\n google-cloud-pubsub\n\n App Engine automatically installs these dependencies during app\n deployment in the Python 3 runtime, so delete the `lib` folder if one\n exists.\n3. For Python 2 apps, if your app is using built-in or copied libraries, you\n must specify those paths in the `appengine_config.py` file, located in the\n same folder as your `app.yaml` file:\n\n import pkg_resources\n from google.appengine.ext import vendor\n\n # Set PATH to your libraries folder.\n PATH = 'lib'\n # Add libraries installed in the PATH folder.\n vendor.add(PATH)\n # Add libraries to pkg_resources working set to find the distribution.\n pkg_resources.working_set.add_entry(PATH)\n\n The `appengine_config.py` file above assumes that the current working\n directory is where the `lib` folder is located. In some cases, such as unit\n tests, the current working directory can be different. To avoid errors, you\n can explicitly pass in the full path to the `lib` folder using: \n\n ```python\n import os\n path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')\n ```\n4. Import the Pub/Sub\n\n Python client library\n\n in any files that use\n pull queues from the Task Queues API:\n\n ```bash\n from google.cloud import pubsub\n ```\n\nPub/Sub and pull queues\n-----------------------\n\n### Feature comparison\n\nPub/Sub sends work to workers through a publisher/subscriber\nrelationship. A [pull subscription](/pubsub/docs/subscriber#pull-subscription)\nin Pub/Sub is like a pull queue in Task Queues\nbecause the subscriber pulls the message from the topic. The table below lists\nthe core feature for pull queues in Task Queues and the\nassociated feature for pull subscriptions in Pub/Sub.\n\nTo learn more about the Pub/Sub architecture, read\n[Cloud Pub/Sub: A Google-Scale Messaging Service](/pubsub/architecture).\n\n### Workflow comparison\n\nBelow is a comparison of a typical workflow for a pull queue in\nTask Queues and a\n[pull subscription](/pubsub/docs/subscriber#pull-subscription)\nin Pub/Sub.\n\nCreating pull subscriptions in Pub/Sub\n--------------------------------------\n\nYou can use a Pub/Sub pull subscription like a\nTask Queues pull queue. Subscriptions to a topic do not expire\nand can exist simultaneously for multiple workers. This means that a message can\nbe processed by more than one worker, which is one of the primary use cases for\nPub/Sub. To recreate your Task Queues pull\nqueues as Pub/Sub pull subscriptions, create a topic for each\nworker and subscribe only the associated worker to the topic. This ensures that\neach message is processed by exactly one worker as in\nTask Queues. To learn about creating and managing pull\nsubscriptions, read about\n[managing topics and subscriptions](/pubsub/docs/admin).\n\nDeleting pull queues\n--------------------\n\nAfter you migrate your Task Queues pull queues to\nPub/Sub pull subscriptions, delete them from\nTask Queues using your `queue.yaml` file. We recommend deleting\neach pull queue before migrating the next one. This prevents your app from\nduplicating the work it receives from the new Pub/Sub pull\nsubscription while you migrate your other pull queues. Note that deleting your\nTask Queues pull queues one by one rather than in a single\ndeployment might have a greater effect on your App Engine\n[deployment quota](/appengine/docs/standard/quotas#Deployments).\n\nAfter you delete all your pull queues from Task Queues, you can\nomit the `queue.yaml` file from future deployments of your app.\n\nIf your app only uses pull queues, remove any references to the\nTask Queues API in your code. If your app uses both pull queues and push\nqueues, you can either remove the references to the Task Queues API that\noccur in files that only use pull queues, or wait until you have also migrated\nyour push queues and then remove references to the Task Queues API from\nall files.\n\nWhat's next\n-----------\n\n- [Pub/Sub documentation](/pubsub/docs)\n- [Migrating push queues](/appengine/migration-center/standard/python/migrating-push-queues)\n- For a hands-on tutorial, see the [Migrate from App Engine Task Queue\n pull queues to Pub/Sub codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-19-pubsub)."]]