-1

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)
kupo2001
  • 7
  • 4

1 Answers1

0

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)
kupo2001
  • 7
  • 4