使用带有apscheduler的Flask

时间:2013-10-30 12:03:53

标签: python flask

我使用Python Flask和apscheduler一起尝试按如下方式添加/删除作业: -

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'

这个位按预期工作。但是,当我尝试删除这样的工作时:

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

我收到了“NameError:全局名称'job_function'未按预期定义”。我的问题是如何使用不同的路线删除作业?

问候。

1 个答案:

答案 0 :(得分:6)

确定排序了它!

对于其他需要这样做的人:

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

然后:

sched.unschedule_job(job_function.job)