如何让apscheduler不双重执行?

发布于 2025-01-10 04:26:27 字数 2039 浏览 0 评论 0原文

我的 apscheduler 正在工作,但由于某种原因,当它写入我的文件时,每次调用 BackgroundScheduler 时都会进行两次输入。

我不明白为什么它会写入 2 行,并且 .txt 文件中的时间戳会变化几毫秒,

我只想要 1 个条目,所以我最终可以将其写入数据库,但我需要了解正在发生什么它双重执行

我没有看到我的代码中的哪个位置会使其双重写入。

请帮忙谢谢高级

main.py

from flask import Flask, render_template
from flask_caching import Cache
from flask_bootstrap import Bootstrap
from flask_ckeditor import CKEditor
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler
from market_data import MarketData
import os

sched = BackgroundScheduler()
sched.start()
current_year = date.today().year
data = MarketData()


def grab_every_five():
    p_data = get_c_data()
    with open(file='data.txt', mode='a', encoding='utf-8') as f:
        f.write(f'{p_data}\n')


sched.add_job(grab_every_five, 'interval', minutes=1)
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
app = Flask(__name__)
cache.init_app(app)

app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
ckeditor = CKEditor(app)
Bootstrap(app)


@app.route('/', methods=['GET', 'POST'])
def home_page():
    c_data = get_c_data()
    return render_template("index.html",
                           year=current_year,
                           asset_count="{:,.0f}".format(c_data['active']),
                           fiat_total="{:,.2f}".format(c_data['total_market_cap']),
                           per_asset="{:,.2f}".format(c_data['value_per_active']),
                           per_all_asset="{:,.2f}".format(c_data['value_per_all']),
                           new_asset="{:,}".format(c_data['total_asset']),
                           country="{:,.2f}".format(c_data['asset_per_country']),
                           all_asset="{:,}".format(c_data['total_asset']),
                           time_stamp = c_data['time_stamp']
                           )


@cache.cached(timeout=60, key_prefix='c_data_cache')
def get_c_data():
    temp_data = data.get_data()
    return temp_data


if __name__ == "__main__":
    app.run(debug=True)

I have the apscheduler working but for some reason when it writes to my file it makes a double-entry every time the BackgroundScheduler is called.

I'm not understanding why it writes 2 lines and the timestamp will be varied by a few milliseconds in the .txt file

I only want the 1 entry, so I can eventually have it written to a database but I need to understand what is making it double executes

I don't see where in my code that it would make it double write.

Please help thank you in advanced

main.py

from flask import Flask, render_template
from flask_caching import Cache
from flask_bootstrap import Bootstrap
from flask_ckeditor import CKEditor
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler
from market_data import MarketData
import os

sched = BackgroundScheduler()
sched.start()
current_year = date.today().year
data = MarketData()


def grab_every_five():
    p_data = get_c_data()
    with open(file='data.txt', mode='a', encoding='utf-8') as f:
        f.write(f'{p_data}\n')


sched.add_job(grab_every_five, 'interval', minutes=1)
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
app = Flask(__name__)
cache.init_app(app)

app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
ckeditor = CKEditor(app)
Bootstrap(app)


@app.route('/', methods=['GET', 'POST'])
def home_page():
    c_data = get_c_data()
    return render_template("index.html",
                           year=current_year,
                           asset_count="{:,.0f}".format(c_data['active']),
                           fiat_total="{:,.2f}".format(c_data['total_market_cap']),
                           per_asset="{:,.2f}".format(c_data['value_per_active']),
                           per_all_asset="{:,.2f}".format(c_data['value_per_all']),
                           new_asset="{:,}".format(c_data['total_asset']),
                           country="{:,.2f}".format(c_data['asset_per_country']),
                           all_asset="{:,}".format(c_data['total_asset']),
                           time_stamp = c_data['time_stamp']
                           )


@cache.cached(timeout=60, key_prefix='c_data_cache')
def get_c_data():
    temp_data = data.get_data()
    return temp_data


if __name__ == "__main__":
    app.run(debug=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2025-01-17 04:26:27

好的,我在这篇文章中找到了答案 Flask 中的 apscheduler 执行了两次

结果是通过调试它的模式让重新加载器还加载 apscheduler 的一个实例

最简单、最快的解决方法是关闭重新加载器,如我链接到的文章中提到的那样,

我只需添加这一行 代码:

app.run(use_reloader=False)

Ok i found the answer in this post apscheduler in Flask executes twice

It turns out with debug mode on it has the reloader also load an instance of the apscheduler

The easiest and fastest fix was to turn off the reloader as mentioned in the article I've linked to

I just had to add this line of code:

app.run(use_reloader=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文