Reloading pickling in a Flask application

I am trying to use the Flask application and want to reload the pickle file in a specific time window (e.g. 9am every day). I tried putting a while loop at the end of my flash application using a time counter, but this ends up hanging my application. Currently setting up ...

# main.wsgi
from main import app as application

# main.py
data = pickle.load("/path/to/pickle.file")
@app.route("/")
def func():
    return render_template("base.html", data_to_serve = data)
# Can I write something here to reload the data at specific time points?
+4
source share
2 answers

, - , " ". - pymemcache Flask, , . , ; , .

, , ; 9:00 12:00. - now.time() == time(hour=9), , .

import pickle

from datetime import datetime, time


cached_data = pickle.load("/path/to/pickle.file")
START_TIME = time(hour=9)
END_TIME = time(hour=12)  # Can also use something like timedelta


def in_range():
  now = datetime.now()
  if START_TIME <= now.time() <= END_TIME:        
      return True
  return False


app.route("/")
def func():
  if in_range():
    return render_template("base.html", data_to_serve = cached_data)

  # else do normal business
  data = 'compute new data...'
  return render_template("base.html", data_to_serve = data)

!

+1

, 2 :

0

Source: https://habr.com/ru/post/1680649/


All Articles