Q:

PermanentTaskFailure in AppEngine deferred library

I am using App Engine and the deferred library. But every once in a while my task fail with the following error:
Permanent failure attempting to execute task
Traceback (most recent call last):
  File "/base/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", 
    line 256, in post
      run(self.request.body)
  File "/base/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", 
    line 122, in run
      raise PermanentTaskFailure(e)
PermanentTaskFailure: Environment variable DJANGO_SETTINGS_MODULE is undefined.

I found a post saying the solution is to:
from google.appengine.ext.webapp import template 

But I am not using the webapp framework, I'm using Django. What can I try?

A:

Errors like this generally occur when your task depends on some Python path manipulation or other jiggery-pokery that isn't being carried out when the first request to a runtime is via deferred. You need to make sure that all your entry points - eg, the module containing the function you're deferring - import a module that does the relevant path manipulation.
Alternatively, you can write your own deferred task handler, which, being part of your framework, will already have the relevant patches applied. For that, simply register the handler and have it call deferred.run(), passing in the complete body of the request.

