Using Flask with apscheduler

I am using Python Flask along with apscheduler and trying to add / remove jobs as follows: -

sched = Scheduler() sched.start() print "Schedular Started" def new_job(): @sched.interval_schedule(seconds=2) def job_function(): print "Hello World" @app.route('/add') def add(): new_job() return 'started' 

This bit works as expected. However, when I try to delete the job as follows:

 @app.route('/remove') def remove(): sched.unschedule_job(job_function.job) return "Removed" 

I get "NameError: global name" job_function "not defined" as expected. My question is: how to delete a job using a different route?

Sincerely.

+7
python flask
source share
1 answer

OK Sorted!

For someone else who needs to do this:

 @sched.interval_schedule(seconds=2) def job_function(): print "Hello World" 

Then:

 sched.unschedule_job(job_function.job) 
+6
source share

All Articles