I'm writing a server-side application in flask / python and have an issue with some data that has to be loaded for calculation. Loading the data (about 40 MB) takes much longer that processing the server response, and the data never changes, so I want it to be loaded only once, effectively when apache starts up. But no matter what I try, it keeps reloading each time a request comes in and massively slowing things down. I can tell by the print statement shown below, which writes to the apache logs for each request. I want to load the data, and so write that line to the logs, only once on startup.
Interestingly, this only happens when the script is run via apache on WSGI - if I run it locally using python from the command line, the data load only happens once, and server responses are much faster.
Any thoughts?
My most recent attempt, using flask_cache, is like this:
@cache.cached(key_prefix = 'my_key')
def load_huge_file():
#Do some things and assign data from a large file to loaded_data
print "Huge data set loaded!"
return loaded_data
shared_data = load_huge_file()
@app.route("/user_input")
def user_response():
global shared_data
return fairly_quick_function(args, shared_data)
Edit - Thanks - using before_first_request and adding "WSGIDaemonProcess myApp processes=1" to my WSGI config did the trick. Now it keeps the process running and just spins new requests off of it, instead of re running the init each time.