Python 2.7 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Python.
Stay organized with collections
Save and categorize content based on your preferences.
google.appengine.ext.deferred.deferred module
Summary
A module that handles deferred execution of callables via the task queue.
Tasks consist of a callable and arguments to pass to it. The callable and its
arguments are serialized and put on the task queue, which deserializes and
executes them. The following callables can be used as tasks:
-
Functions defined in the top level of a module
-
Classes defined in the top level of a module
-
Instances of classes in (2) that implement __call__
-
Instance methods of objects of classes in (2)
-
Class methods of classes in (2)
-
Built-in functions
-
Built-in methods
The following callables can NOT be used as tasks:
1) Nested functions or closures
2) Nested classes or objects of them
3) Lambda functions
4) Static methods
The arguments to the callable, and the object (in the case of method or object
calls) must all be pickleable.
If you want your tasks to execute reliably, don’t use mutable global variables;
they are not serialized with the task and may not be the same when your task
executes as they were when it was enqueued (in fact, they will almost certainly
be different).
If your app relies on manipulating the import path, make sure that the function
you are deferring is defined in a module that can be found without import path
manipulation. Alternately, you can include deferred.TaskHandler in your own
webapp application instead of using the easy-install method detailed below.
When you create a deferred task using deferred.defer, the task is serialized,
and an attempt is made to add it directly to the task queue. If the task is too
big (larger than about 10 kilobytes when serialized), a datastore entry will be
created for the task, and a new task will be enqueued, which will fetch the
original task from the datastore and execute it. This is much less efficient
than the direct execution model, so it’s a good idea to minimize the size of
your tasks when possible.
In order for tasks to be processed, you need to set up the handler. Add the
following to your app.yaml handlers section:
handlers:
- url: /_ah/queue/deferred
script: $PYTHON_LIB/google/appengine/ext/deferred/handler.py
login: admin
By default, the deferred module uses the URL above, and the default queue.
Example usage:
def do_something_later(key, amount):
entity = MyModel.get(key)
entity.total += amount
entity.put()
# Use default URL and queue name, no task name, execute ASAP.
deferred.defer(do_something_later, my_key, 20)
# Providing non-default task queue arguments
deferred.defer(do_something_later, my_key, 20, _queue="foo", _countdown=60)
Contents
- exception google.appengine.ext.deferred.deferred.Errorsource
-
Bases: exceptions.Exception
Base class for exceptions in this module.
- class google.appengine.ext.deferred.deferred.TaskHandlersource
-
Bases: google.appengine.ext.webapp._webapp25.RequestHandler
A webapp handler class that processes deferred invocations.
- run_from_request()source
-
Default behavior for POST requests to deferred handler.
- google.appengine.ext.deferred.deferred.defer(obj, *args, **kwargs)source
Defers a callable for execution later.
The default deferred URL of /_ah/queue/deferred will be used unless an
alternate URL is explicitly specified. If you want to use the default URL for
a queue, specify _url=None. If you specify a different URL, you will need to
install the handler on that URL (see the module docstring for details).
Parameters
-
obj – The callable to execute. See module docstring for restrictions.
_countdown, _eta, _headers, _name, _target, _transactional, _url,
_retry_options, _queue: Passed through to the task queue - see the
task queue documentation for details.
-
args – Positional arguments to call the callable with.
-
kwargs – Any other keyword arguments are passed through to the callable.
ReturnsA taskqueue.Task object which represents an enqueued callable.
- google.appengine.ext.deferred.deferred.invoke_member(obj, membername, *args, **kwargs)source
Retrieves a member of an object, then calls it with the provided arguments.
Parameters
-
obj – The object to operate on.
-
membername – The name of the member to retrieve from ojb.
-
args – Positional arguments to pass to the method.
-
kwargs – Keyword arguments to pass to the method.
ReturnsThe return value of the method invocation.
- google.appengine.ext.deferred.deferred.main()source
- google.appengine.ext.deferred.deferred.run(data)source
Unpickles and executes a task.
Parametersdata – A pickled tuple of (function, args, kwargs) to execute.
ReturnsThe return value of the function invocation.
- google.appengine.ext.deferred.deferred.run_from_datastore(key)source
Retrieves a task from the datastore and executes it.
Parameterskey – The datastore key of a _DeferredTaskEntity storing the task.
ReturnsThe return value of the function invocation.
- google.appengine.ext.deferred.deferred.serialize(obj, *args, **kwargs)source
Serializes a callable into a format recognized by the deferred executor.
Parameters
-
obj – The callable to serialize. See module docstring for restrictions.
-
args – Positional arguments to call the callable with.
-
kwargs – Keyword arguments to call the callable with.
ReturnsA serialized representation of the callable.
- google.appengine.ext.deferred.deferred.set_log_level(log_level)source
Sets the log level deferred will log to in normal circumstances.
Parameterslog_level – one of logging log levels, e.g. logging.DEBUG, logging.INFO, etc.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003eThe \u003ccode\u003edeferred\u003c/code\u003e module allows for the delayed execution of callables via the task queue, serializing functions, classes, and their arguments to be run later.\u003c/p\u003e\n"],["\u003cp\u003eOnly top-level functions, classes, class instances implementing \u003ccode\u003e__call__\u003c/code\u003e, instance methods, class methods, and built-in functions/methods are supported as deferred tasks, while nested functions, nested classes, lambda functions, and static methods are not.\u003c/p\u003e\n"],["\u003cp\u003eTo use deferred tasks, ensure that all arguments and objects passed to the callable are pickleable, and avoid using mutable global variables, as they are not serialized with the task.\u003c/p\u003e\n"],["\u003cp\u003eFor processing tasks, a handler must be set up in the \u003ccode\u003eapp.yaml\u003c/code\u003e file, and the default URL is \u003ccode\u003e/_ah/queue/deferred\u003c/code\u003e, although custom URLs can also be used with appropriate handler configurations.\u003c/p\u003e\n"],["\u003cp\u003eTasks larger than approximately 10 kilobytes when serialized are stored in the datastore and then retrieved for execution, which is less efficient than direct execution.\u003c/p\u003e\n"]]],[],null,["# google.appengine.ext.deferred.deferred module\n=============================================\n\nSummary\n-------\n\nA module that handles deferred execution of callables via the task queue.\n\nTasks consist of a callable and arguments to pass to it. The callable and its\narguments are serialized and put on the task queue, which deserializes and\nexecutes them. The following callables can be used as tasks:\n\n1. Functions defined in the top level of a module\n\n2. Classes defined in the top level of a module\n\n3. Instances of classes in (2) that implement __call__\n\n4. Instance methods of objects of classes in (2)\n\n5. Class methods of classes in (2)\n\n6. Built-in functions\n\n7. Built-in methods\n\nThe following callables can NOT be used as tasks:\n1) Nested functions or closures\n2) Nested classes or objects of them\n3) Lambda functions\n4) Static methods\n\nThe arguments to the callable, and the object (in the case of method or object\ncalls) must all be pickleable.\n\nIf you want your tasks to execute reliably, don't use mutable global variables;\nthey are not serialized with the task and may not be the same when your task\nexecutes as they were when it was enqueued (in fact, they will almost certainly\nbe different).\n\nIf your app relies on manipulating the import path, make sure that the function\nyou are deferring is defined in a module that can be found without import path\nmanipulation. Alternately, you can include deferred.TaskHandler in your own\nwebapp application instead of using the easy-install method detailed below.\n\nWhen you create a deferred task using deferred.defer, the task is serialized,\nand an attempt is made to add it directly to the task queue. If the task is too\nbig (larger than about 10 kilobytes when serialized), a datastore entry will be\ncreated for the task, and a new task will be enqueued, which will fetch the\noriginal task from the datastore and execute it. This is much less efficient\nthan the direct execution model, so it's a good idea to minimize the size of\nyour tasks when possible.\n\nIn order for tasks to be processed, you need to set up the handler. Add the\nfollowing to your app.yaml handlers section:\n\nhandlers:\n- url: /_ah/queue/deferred\n\nscript: $PYTHON_LIB/google/appengine/ext/deferred/handler.py\nlogin: admin\n\nBy default, the deferred module uses the URL above, and the default queue.\n\nExample usage: \n\n def do_something_later(key, amount):\n entity = MyModel.get(key)\n entity.total += amount\n entity.put()\n\n # Use default URL and queue name, no task name, execute ASAP.\n deferred.defer(do_something_later, my_key, 20)\n\n # Providing non-default task queue arguments\n deferred.defer(do_something_later, my_key, 20, _queue=\"foo\", _countdown=60)\n\nContents\n--------\n\n*exception* google.appengine.ext.deferred.deferred.Error[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#Error)\n\n: Bases: exceptions.Exception\n\nBase class for exceptions in this module. \n\n*exception* google.appengine.ext.deferred.deferred.PermanentTaskFailure[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#PermanentTaskFailure)\n\n: Bases: [google.appengine.ext.deferred.deferred.Error](#google.appengine.ext.deferred.deferred.Error)\n\nIndicates that a task failed, and will never succeed. \n\n*exception* google.appengine.ext.deferred.deferred.SingularTaskFailure[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#SingularTaskFailure)\n\n: Bases: [google.appengine.ext.deferred.deferred.Error](#google.appengine.ext.deferred.deferred.Error)\n\nIndicates that a task failed once. \n\n*class* google.appengine.ext.deferred.deferred.TaskHandler[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#TaskHandler)\n\n: Bases: google.appengine.ext.webapp._webapp25.RequestHandler\n\n A webapp handler class that processes deferred invocations. \n\n post()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#TaskHandler.post)\n : \n\n run_from_request()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#TaskHandler.run_from_request)\n\n : Default behavior for POST requests to deferred handler.\n\ngoogle.appengine.ext.deferred.deferred.defer(obj, \\*args, \\*\\*kwargs)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#defer)\n\n: Defers a callable for execution later.\n\n The default deferred URL of /_ah/queue/deferred will be used unless an\n alternate URL is explicitly specified. If you want to use the default URL for\n a queue, specify _url=None. If you specify a different URL, you will need to\n install the handler on that URL (see the module docstring for details).\n Parameters\n\n - obj -- The callable to execute. See module docstring for restrictions.\n _countdown, _eta, _headers, _name, _target, _transactional, _url,\n _retry_options, _queue: Passed through to the task queue - see the\n task queue documentation for details.\n\n - args -- Positional arguments to call the callable with.\n\n - kwargs -- Any other keyword arguments are passed through to the callable.\n\n Returns\n\nA taskqueue.Task object which represents an enqueued callable. \n\ngoogle.appengine.ext.deferred.deferred.invoke_member(obj, membername, \\*args, \\*\\*kwargs)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#invoke_member)\n\n: Retrieves a member of an object, then calls it with the provided arguments.\n\n Parameters\n\n - obj -- The object to operate on.\n\n - membername -- The name of the member to retrieve from ojb.\n\n - args -- Positional arguments to pass to the method.\n\n - kwargs -- Keyword arguments to pass to the method.\n\n Returns\n\nThe return value of the method invocation. \n\ngoogle.appengine.ext.deferred.deferred.main()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#main)\n: \n\ngoogle.appengine.ext.deferred.deferred.run(data)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#run)\n\n: Unpickles and executes a task.\n\n Parameters\n\n data -- A pickled tuple of (function, args, kwargs) to execute.\n Returns\n\nThe return value of the function invocation. \n\ngoogle.appengine.ext.deferred.deferred.run_from_datastore(key)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#run_from_datastore)\n\n: Retrieves a task from the datastore and executes it.\n\n Parameters\n\n key -- The datastore key of a _DeferredTaskEntity storing the task.\n Returns\n\nThe return value of the function invocation. \n\ngoogle.appengine.ext.deferred.deferred.serialize(obj, \\*args, \\*\\*kwargs)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#serialize)\n\n: Serializes a callable into a format recognized by the deferred executor.\n\n Parameters\n\n - obj -- The callable to serialize. See module docstring for restrictions.\n\n - args -- Positional arguments to call the callable with.\n\n - kwargs -- Keyword arguments to call the callable with.\n\n Returns\n\nA serialized representation of the callable. \n\ngoogle.appengine.ext.deferred.deferred.set_log_level(log_level)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/deferred/deferred#set_log_level)\n\n: Sets the log level deferred will log to in normal circumstances.\n\n Parameters\n\n log_level -- one of logging log levels, e.g. logging.DEBUG, logging.INFO, etc."]]