使用Flask运行计划时出现的问题 [英] Issue when running schedule with Flask

查看:250
本文介绍了使用Flask运行计划时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Flask应用程序上定期运行某些任务.我决定使用一个简单的库-Schedule( https://github.com/dbader/schedule )这样做.我在与主应用程序线程不同的线程上运行任务计划程序.这是相关的代码段.

I need to run a certain task periodically on my Flask application. I decided to use a simple library - Schedule (https://github.com/dbader/schedule) for doing this. I am running the task scheduler on a separate thread from the main application thread. Here's the relevant code snippet.

import schedule
import time

from flask import Flask, request
from threading import Thread

app = Flask(__name__)

start_time = time.time()

def run_every_10_seconds():
    print("Running periodic task!")
    print "Elapsed time: " + str(time.time() - start_time)

def run_schedule():
    while 1:
        schedule.run_pending()
        time.sleep(1)   

@app.route('/', methods=['GET'])
def index():
    return '<html>test</html>'

if __name__ == '__main__':
    schedule.every(10).seconds.do(run_every_10_seconds)
    t = Thread(target=run_schedule)
    t.start()
    print "Start time: " + str(start_time)
    app.run(debug=True, host='0.0.0.0', port=5000)

当我运行此程序时,我想要正在运行定期任务!"每10秒打印一次.但是,这是我得到的输出.

When I run this, I'd like 'Running periodic task!' to print every 10 seconds. However, this is the output I get.

 * Running on http://0.0.0.0:5000/
 * Restarting with reloader
Start time: 1417002869.99
Running periodic task!
Elapsed time: 10.0128278732
Running periodic task!
Elapsed time: 10.0126948357
Running periodic task!
Elapsed time: 20.0249710083
Running periodic task!
Elapsed time: 20.0247309208
Running periodic task!
Elapsed time: 30.0371530056
Running periodic task!
Elapsed time: 30.0369319916

很显然,由于某种原因,任务似乎每10秒执行两次,而不是一次.但是,如果我仅单独运行任务计划程序,而不是与Flask一起运行(仅通过注释app.run()行),则它可以正常运行.

Clearly, for some reason, the task seems to be executing twice every 10 seconds, instead of once. However, if I run merely the task scheduler alone instead of running it alongside Flask (by simply commenting the app.run() line), it runs properly.

Start time: 1417003801.52
Running periodic task!
Elapsed time: 10.0126750469
Running periodic task!
Elapsed time: 20.0246500969
Running periodic task!
Elapsed time: 30.0366458893

这可能是什么原因?运行多个线程时,任务排队的方式是否存在问题?仍然没有解释为什么为什么一次只安排两项任务.

What could be the reason behind this? Is there a problem with the way the tasks are queued when running multiple threads? It still doesn't explain why two tasks are being scheduled at a time when only one should be.

推荐答案

使用重新加载程序运行开发服务器时(默认为debug=True),模块执行两次,导致两个t实例.您可以通过添加print(id(t))进行验证.

When you run the development server with the reloader (the default when debug=True), the module executes twice, causing two instances of t. You can verify this by adding print(id(t)).

解决此问题的最简单方法是将use_reloader=False传递给app.run.您可以查看此答案,以获取允许您使用重新加载器的替代解决方案.

The simplest way around this is to pass use_reloader=False to app.run. You can see this answer for an alternative solution that allows you to use the reloader.

这篇关于使用Flask运行计划时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆