如何阻止烧瓶重新加载代码两次?

时间:2017-11-12 14:06:14

标签: python flask werkzeug apscheduler

有问题,但我有另一个问题。

我试过了:

app.run(use_reloader=False)

如:

from flask import Flask, render_template
from scheduler import *
from regions import regions

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html", regions=regions)

if __name__ == "__main__":
    app.run(use_debugger=False, use_reloader=False)

但没有任何反应。它只是继续重装。

~/workspace/finalproject/weather/ $ flask run
* Serving Flask app "sync"
* Forcing debug mode off
* Running on http://ide50-artemnikitin27ed.cs50.io:8080/ (Press CTRL+C to quit)
* Restarting with stat
INFO:apscheduler.scheduler:Scheduler started
DEBUG:apscheduler.scheduler:Looking for jobs to run
DEBUG:apscheduler.scheduler:No jobs; waiting until a job is added
INFO:apscheduler.scheduler:Scheduler started
DEBUG:apscheduler.scheduler:Looking for jobs to run
DEBUG:apscheduler.scheduler:No jobs; waiting until a job is added

我甚至关闭了调试模式,但重启器仍然在启动时继续运行代码两次。

我需要这个,以便阻止Apscheduler运行两次,这完全有效,期待这个问题。

我将非常感谢您的帮助!

SOLUTION:

要停用重新加载器,如果您使用flask run执行flask run --no-reload而不是在答案中https://stackoverflow.com/a/25504196/8882620

要在不阻止重新加载器的情况下解决问题,请将apscheduler设置放在函数中并用@app.before_first_request包装:

@app.before_first_request
def before_first_request():
   scheduler = BackgroundScheduler()
   scheduler.start()
   scheduler.add_job(
   func=historical,
   trigger=CronTrigger(hour=12, minute=12),
      id='save_weather',
      name='Archive avg temp for current day',
      replace_existing=True)

   # Shut down the scheduler when exiting the app
   atexit.register(lambda: scheduler.shutdown())

0 个答案:

没有答案