Flask - Store values in memory between requests Flask - Store values in memory between requests flask flask

Flask - Store values in memory between requests


Storing file in this way is not going to work if your webserver is spawning multiple processes (workers) to handle requests, and that is how most production servers works.

Further to keep file object in memory is not going to scale if your server load increases, you can either save file in file system and initialize the pandas object during every requests. You can compare this with loading pickled object and see which is faster. You will also have to consider overhead of pickling not just unpickling.

EDIT: explanation of why it wont work in production

Gunicorn and similar webservers are likely to spawn multiple workers unless you are restricting in config, a worker is essentially a separate process and each process has its own python execution environment. So lets say your first request hits worker1 and you create a variable current_app.file = file_in_memory in that process. Then your second request could hit worker2 which has its own python execution environment where your variable is not available because they are not shared across processes. In fact there might be a value in that variable but it belongs to different user request.

So all in all

  1. It does not guarantee that same object is available across requests
  2. It could get overridden by another user who is also simultaneously using your app


Although I am answering the question very late, this would still help many of us. For sharing values between requests you should trust on using cache memory. Cache memory in flask is simple to implement using flask-cache and redis as database. It would be most efficient and reliable way of doing it. Read any article on implementation of redis as cache db in flask for further reference.