Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask with apscheduler

Tags:

python

flask

I using Python Flask to 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 remove the job, like shown here:

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

I'm getting a

NameError: global name 'job_function' is not defined" as expected.

My question is how can I remove a job using a different route?

Regards.

like image 555
user1513388 Avatar asked Oct 30 '13 12:10

user1513388


1 Answers

OK Sorted it!

For anybody else that needs to do this:

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

Then:

sched.unschedule_job(job_function.job)
like image 85
user1513388 Avatar answered Oct 05 '22 15:10

user1513388