Can web2py have two scheduler tasks?

95 views
Skip to first unread message

Aydin S

unread,
Jul 5, 2015, 9:15:35 AM7/5/15
to web...@googlegroups.com
I'm trying to create a second model and use scheduler to run it periodically, but an error happens that the table exist already. I use this at the end of the model:
from gluon.scheduler import Scheduler
Scheduler(db,dict(function=f))

And the error is correct because the first model that has the above code has already created a table called scheduler_task.table and its relative tables.
My question is is there a way to name the tables differently so that I could run two separate models using scheduler?

Anthony

unread,
Jul 5, 2015, 10:44:18 AM7/5/15
to web...@googlegroups.com, hsof...@gmail.com
Once the scheduler has been defined, you can run tasks from anywhere. Why do you need to define it again in another model file?

Aydin S

unread,
Jul 5, 2015, 11:15:56 AM7/5/15
to web...@googlegroups.com, hsof...@gmail.com
I'm trying to run two different function with two different periodic times. I think what you mean is that I still can do it using
from gluon.scheduler import Scheduler
Scheduler(db,dict(function1=f1,function2=f2))

and then in the schedule use function1 and function2 to create two different periodic tasks. I've tried this, and it did not work.

Anthony

unread,
Jul 5, 2015, 3:08:43 PM7/5/15
to web...@googlegroups.com, hsof...@gmail.com
On Sunday, July 5, 2015 at 11:15:56 AM UTC-4, Aydin S wrote:
I'm trying to run two different function with two different periodic times. I think what you mean is that I still can do it using
from gluon.scheduler import Scheduler
Scheduler(db,dict(function1=f1,function2=f2))

and then in the schedule use function1 and function2 to create two different periodic tasks. I've tried this, and it did not work.

Please read the Scheduler documentation: http://web2py.com/books/default/chapter/29/04/the-core#web2py-Scheduler.

Your second line should be something like:

scheduler = Scheduler(db, ...)

otherwise, you will have no way to refer to the Scheduler object later in your code when you want to schedule a task.

Just make sure function1 and function2 have been defined (or imported from a module) before the above lines. If for some reason you need to define one of the functions later, note that the second argument to Scheduler() is optional (i.e., you don't have to provide a dictionary mapping task names to functions -- you can instead pass a function directly to scheduler.queue_task when you actually queue a task). I think you can also update the task dictionary at a later point:

scheduler.tasks.update('new_task'=new_function)

Anthony

Aydin S

unread,
Jul 5, 2015, 4:48:35 PM7/5/15
to web...@googlegroups.com, hsof...@gmail.com
Thanks for the guidance, it worked after correcting the format I was creating new tasks.
Reply all
Reply to author
Forward
0 new messages