Using Flask with apscheduler

1,486 views
Skip to first unread message

Carl Bourne

unread,
Oct 30, 2013, 10:31:25 AM10/30/13
to apsch...@googlegroups.com
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 this:

    @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 from the schedular using a different route?

Regards.

Alex Grönholm

unread,
Oct 30, 2013, 11:13:47 AM10/30/13
to apsch...@googlegroups.com
Why is your job function not global? It won't be accessible from outside
the new_job() function. Make it global and use the add_interval_job()
method instead of the decorator.
>
> Regards.
>
> --
> You received this message because you are subscribed to the Google
> Groups "APScheduler" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to apscheduler...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Carl Bourne

unread,
Oct 30, 2013, 11:51:41 AM10/30/13
to apsch...@googlegroups.com
Arr OK that makes sense! 

This is what I now have: 

sched = Scheduler()
sched.start()
print "Schedular Started"

def my_job():
    print "Hello World"

@app.route('/add')
def add():
    sched.add_interval_job(my_job, seconds=5)
    return 'added'

@app.route('/remove')
def remove():
    sched.unschedule_func(my_job)
    return "removed"

The strange thing now though is that if I use:

>>> sched.unschedule_job(my_job.job)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'function' object has no attribute 'job'

However 

>>>sched.unschedule_function(my_job)

Works for me! 

Carl Bourne

unread,
Oct 30, 2013, 12:27:22 PM10/30/13
to apsch...@googlegroups.com
Just curios, but any idea why sched.unschedule_function(my_job) works here and sched.unschedule_job(my_job.job) does not?

Carl

 
On Wednesday, 30 October 2013 14:31:25 UTC, Carl Bourne wrote:

Alex Grönholm

unread,
Oct 30, 2013, 1:16:08 PM10/30/13
to apsch...@googlegroups.com
30.10.2013 18:27, Carl Bourne kirjoitti:
Just curios, but any idea why sched.unschedule_function(my_job) works here and sched.unschedule_job(my_job.job) does not?

What is "my_job" in this context?
Carl
 
On Wednesday, 30 October 2013 14:31:25 UTC, Carl Bourne wrote:
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 this:

    @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 from the schedular using a different route?

Regards.

Reply all
Reply to author
Forward
0 new messages