I recently started using deferred tasks in my App Engine application. I’m using them mainly for two things:
- splitting user requests into two pieces – one that being done immediately and one that being done in the background, to speed up the response time.
- mapping (iterating over) all my entities to calculate various statistics.
I won’t provide here details on how to use deferred tasks, because this is described in detail in a great article by Nick Johnson, which I highly recommend reading. One thing I do want to share from my experience, is my implementation of the Mapper base class:
This implementation is taken from Nick’s article mentioned above, but I made some changes to it. The first change is giving subclasses the ability to set the property to order the entities by. I needed this, because I found out that when your entities have custom key names ordering by key breaks for some reason.
The other change is re-factoring the way the next batch starts. It seems to me that in the sample provided in the article there’s an error in the indentation, resulting in the code not doing batching properly and can even break in some situations. In my version every task run maps only one batch (or less). This is sub optimal in terms of # of tasks it takes to map all your entities, but as long as you don’t have really a lot of entities this is not such an issue.
I really would love to hear others’ comments no my changes and about how you use deferred tasks or map your entities.
Arik