'How can I refresh the data in the background of a running flask app?

I have a simple flask app that queries a database to write a csv then pyplot to create a chart out of that.

I would like to refresh the data in the background every 10 minutes while the app is running. The page doesn't need to refresh the html automatically. It just needs to have fresh data when someone opens the page.

Can I do that in a single script? Or do I need to run a different script outside in crontab or something?

I would just kick over the container every 10 minutes but it takes about 5 minutes to get the query, so that's a 5 minute outage. Not a great idea. I'd prefer it to fetch in the background.

Here is what I'm working with:

import os
from datetime import date
import teradatasql
import pandas as pd
import matplotlib.pyplot as plt
from flask import Flask, render_template
import time
import multitasking

### variables 
ausername = os.environ.get('dbuser')
apassword = os.environ.get('dbpassword')
ahost = os.environ.get('dbserver')
systems = ["prd1", "prd2", "frz1", "frz2", "devl"]
qgsystems = ["", "@Tera_Prd2_v2", "@Tera_Frz1_v2", "@Tera_Frz2_v2", "@Tera_Devl_v2"]
weeks = ["0", "7", "30"]
query = """{{fn teradata_write_csv({system}_{week}_output.csv)}}select  (bdi.infodata) as sysname,
                to_char (thedate, 'MM/DD' ) || ' ' || Cast (thetime as varchar(11)) as Logtime,
                sum(drc.cpuuexec)/sum(drc.secs) (decimal(7,2)) as "User CPU", 
                sum(drc.cpuuserv)/sum(drc.secs) (decimal(7,2)) as "System CPU",
                sum(drc.cpuiowait)/sum(drc.secs) (decimal(7,2)) as "CPU IO Wait"
            from dbc.resusagescpu{qgsystem} as drc 
            left outer join boeing_tables.dbcinfotbl{qgsystem} as bdi 
            on bdi.infokey = 'sysname' 
            where drc.thedate >= (current_date - {week})
            order by  logtime asc
            Group by sysname,logtime
;
"""

### functions
@multitasking.task
def fetch(system,qgsystem,week):
    with teradatasql.connect (host=ahost, user=ausername, password=apassword) as con:
        with con.cursor () as cur:
            cur.execute (query.format(system=system, qgsystem=qgsystem, week=week))
            [ print (row) for row in cur.fetchall () ]
@multitasking.task
def plot(system,week):
    for week in weeks: 
        for system in systems:
            df = pd.read_csv(system + "_" + week + "_output.csv")
            df.pop('sysname')
            df.plot.area(x="Logtime")
            figure = plt.gcf()
            figure.set_size_inches(12, 6)
            plt.savefig( "/app/static/" + system + "_" + week + "_webchart.png", bbox_inches='tight', dpi=100)

### main
for week in weeks: 
    for system, qgsystem in zip(systems, qgsystems):
        fetch(system,qgsystem,week)
for week in weeks: 
    for system in systems:
        plot(system,week)
app = Flask(__name__,template_folder='templates')
@app.route('/')
def index():
    return render_template("index.html")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source