4

I'm trying to serve a Flask app and would like to reload a pickle file at a specific time window (e.g. 9AM every day). I've tried to put a while loop into the end of my flask app with a time counter, but this ends up hanging my application. Currently the set up is...

# 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?
thestatnoob
  • 193
  • 2
  • 8

2 Answers2

1

I am assuming the goal here is to do what I call a "poor man's cache". Ideally you'd opt to use something like pymemcache and Flask's cache utils, but the snippet below will accomplish what you want. You can refactor this if you want to reload the pickle each time; kind of would be defeating the purpose I think.

Additionally, note that I have used a range of time to return the pickle data; 9 AM to 12 PM. You can also do something like if now.time() == time(hour=9) to accomplish what you want.

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)

Happy coding!

BhargavPatel
  • 173
  • 7
  • That's a pretty neat solution :) to be fair, I did end up finding this in the end (https://stackoverflow.com/questions/13521798/flask-mod-wsgi-automatic-reload-on-source-code-change) which, coupled with a cron job, is closer to what I want since I would have set up a cron job anyway to do the data update. Will bear this in mind for the future though, thanks so much! – thestatnoob Jul 05 '17 at 10:26
0

You want to reload the data at specific point of time then you have 2 options:

  1. Do it from the client size using javascript and ajax requests using some timer.
  2. Use web sockets. There is a library for flask called flask-socketio. There is an example on how to use it.
Nurjan
  • 5,889
  • 5
  • 34
  • 54