apscheduler in Flask executes twice [duplicate]

2019-01-22 11:47发布

This question already has an answer here:

I have problem when i am using apscheduler in my flask application.

In my view.py file i am writing like this

import time
from apscheduler.scheduler import Scheduler

def test_scheduler():
     print "TEST"
     print time.time()


sched = Scheduler()
sched.add_interval_job(test_scheduler, seconds=5)
sched.start()

And then this method test_scheduler() executes twice in every five second

TEST 1360844314.01 TEST 1360844314.2

5条回答
可以哭但决不认输i
2楼-- · 2019-01-22 12:19

the best solution is to use add_cron_job('*') instead of add_interval_job('*')

查看更多
叛逆
3楼-- · 2019-01-22 12:20

I've made it, I added in add_interval_job parameter to start after a certain time point

sched.add_interval_job(test_scheduler, seconds=5, start_date='2013-02-13 00:00')
查看更多
Anthone
4楼-- · 2019-01-22 12:32

When using the reloader, there are the master and child processes. Your scheduler thread runs in both. You need to prevent the scheduler from running in the master process

if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
  sched = Scheduler()
  sched.add_interval_job(test_scheduler, seconds=5)
  sched.start()
查看更多
放我归山
5楼-- · 2019-01-22 12:34

In debug mode, Flask's reloader will load the flask app twice (How to stop Flask from initialising twice in Debug Mode?). I'm not sure why this is, but it causes apscheduler's jobs to be scheduled twice. A quick print "loaded scheduler" right before sched.start() confirms this.

There are a couple ways around this, as mentioned in the linked answer. The one I found that worked best is just to disable the reloader like so:

app.run(use_reloader=False)

It means I have to reload my app manually as I develop it, but it's a small price to pay to get apscheduler working.

查看更多
女痞
6楼-- · 2019-01-22 12:38

You can start the scheduler in Flask's before_first_request() decorator, which "registers a function to be run before the first request to this instance of the application".

import time
import atexit

from apscheduler.schedulers.background import BackgroundScheduler


def print_date_time():
    print(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))


@app.before_first_request
def init_scheduler():
    scheduler = BackgroundScheduler()
    scheduler.add_job(func=print_date_time, trigger="interval", seconds=3)
    scheduler.start()
    # Shut down the scheduler when exiting the app
    atexit.register(lambda: scheduler.shutdown())

Note that before_first_request() will always be called again with the first request after server reload.

查看更多
登录 后发表回答