I'm running a Flask API on an Ubuntu 20.04 server. The API pulls data from a pickled file. I've noticed that you have to restart the service for the Flask app to update with the new data from the file. Is there a way to have it pull from the file every time it receives a request?
Asked
Active
Viewed 41 times
1 Answers
-1
What you want is to move your pickle loading code to a decorator, and then decorate the functions that need the fresh pickle with that decorator.
There's a lot of information to be found on decorators, but here's a small example:
def preload(func):
def preload_wrapper(*args, **kwargs):
print("put your pickle load code here.")
return func(*args, **kwargs)
return preload_wrapper
@preload
def im_a_http_get_function():
print("you can see the order in which this gets executed")
# call the funciton for testing
im_a_http_get_function()
output
put your pickle load code here.
you can see the order in which this gets executed

Edo Akse
- 4,051
- 2
- 10
- 21