GAE offers a taskqueue module to allow tasks to be run in background. Each
item added to the taskqueue is a redirect to a web2py URL (or any GAE URL)
with parameters (equivalent to web2py request.vars). The task is scheduled
and executed in background by the GAE environment.
This is useful if you want to do additional operations that can complete
after the current action is completed (eg updating a ratings or popularity
table), and you do not want to slow down the current action. The GAE SDK
supports taskqueue and executes tasks in much the same way as a production
GAE application.
GAE taskqueue is not available in a non-GAE environment.
The following is a simple working example:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
def gen_task():
from google.appengine.api import taskqueue
# .... do some things, then
taskqueue.add(url=URL('task_do'),
params=dict(old='original text', new='new text'))
return dict(.....)
def task_do():
db.mytable.insert(old=request.vars['old'], new=request.vars['new'])
concepts
- action gen_task is executed by a user. as well as other possible
operations, this action adds a task to the task queue. This task will call
action 'task_do', with vars as in the params dictionary.
- task_do is executed by the GAE environment after gen_task, at a time
determined by the GAE environment. This may be while other application
actions are occurring.
- task_do inserts a record in a table, but may carry out any valid
operations.
- the web2py helper URL builds the url of the target action as shown in
this example if the action is in the same controller. The target action may
be in a different controller or even different application (in fact any
valid url). Use the c and a arguments to specify a web2py target action in
a different controller/application.
*Note* taskqueue.add(URL('myfunction', vars=dict(var1=v1, var2=v2, ...))
will not work as the variables must be passed through the tasqueue.add
params keyword argument .
--