Excellent answer!

one more thing for the ambitious - if you have a long running task that 
might hit the google timeout, or that you want to take off of the web 
serving instances you can target taskqueues to backend instances.  
https://developers.google.com/appengine/docs/python/backends/

i have some tasks that i run nightly via cron + taskqueues that i run on 
backends.

cfh

On Saturday, September 29, 2012 4:26:30 PM UTC-7, Peter Etchells wrote:
>
> 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 .
>
>

-- 



Reply via email to