This page describes how to use the Deferred API, one of the legacy bundled services, with the Python 3 runtime for the standard environment. Your app can access the bundled services through the App Engine services SDK for Python 3.
Overview
Previously, the Deferred package google.appengine.ext.deferred
depended on the webapp framework in Python 2. Since the webapp framework has
been removed in the App Engine services SDK for Python 3, you need to
make some changes when upgrading your Python 2 app to Python 3.
Enabling the Deferred API
To enable the Deferred API for Python 3, you no longer need to set
builtins.deferred
to on
in the app.yaml
file. Instead, to enable the API, you must pass
use_deferred=True
in the call to wrap_wsgi_app()
.
Similarites and differences
By default, the Deferred API for Python 3 uses the same URL /_ah/queue/deferred
and the same default queue
as it did in Python 2. Note that for apps migrating to Cloud Tasks, the default queue is
not created automatically
and the deferred tasks library is not available.
If your app uses the default /_ah/queue/deferred
endpoint, using
deferred.defer()
in Python 3
remains the same as
Python 2.
If your app uses a custom URL for execution of deferred tasks, you need to make
some changes since the TaskHandler
class in the deferred
module for Python 2
has been removed in the Python 3 version of this API.
To set a custom URL for execution of deferred tasks, the app can override either
the post
or the run_from_request
method in the
deferred.Handler
class
(formerly deferred.TaskHandler
in Python 2), and pass the environ
parameter
which represents a dictionary containing WSGI request parameters. The post
method can then be
called from the custom endpoint (as shown in the Python 3 samples).
The end-to-end usage of the Python 3 Deferred API, such as routing of requests and
accessing the environ
dictionary,
depends on the web framework the app is migrating to. Compare code changes made
from the Python 2 example to the Python 3 examples in the following sections.
Python 3 examples
The following example shows how to execute a deferred task using a default endpoint and a custom endpoint in a Flask app and Django app.
Flask
Django
Without any framework
Code samples
To view the complete code samples from this guide, see GitHub.