How to pass complex objects through functions / view sessions in Flask

I am writing a web application that receives a lot of data from a third-party server when (and only when) a user logs in. This data is parsed into user objects and stored in list() . Now the user works with this data throughout the application, calling different views (for example, sending different requests). I'm not sure what is the best template for passing a list of objects between view functions?

I technically see two possibilities, but I have flaws in my case:

  • Session dict . Saving data in a session is redundant (the entire list will be sent back and forth between the server and the browser with each request)
  • Saving . Temporarily stored data in the database seems more appropriate. But I was hoping not to use the database at all (with the exception of this temporary data, I have no data that needs to be stored locally, everything else was received from a third-party server and sent back).

I'm not a very experienced web developer, so maybe I'm watching the obvious. So is there another way to pass data between requests? Maybe some kind of built-in flash magic or is saved (in a file or database) really the only option?

+8
python flask
source share
2 answers

Although the default implementation of session flasks is to store data in a cookie, this is not the only way to do this. Typically, you store the session identifier in a cookie, and the data itself is stored somewhere on the server and retrieved through this cookie.

Flux provides you with an easy way to override the default session implementation, and there are various recipes showing how to do this - here's the SO question that shows the outline.

+5
source share

You can use Flask-Cache ( flask.ext.cache ) SimpleCache (which is really werkzeug.contrib.cache.SimpleCache ) to store data, memory in your application. The only problem is that you will have one cache instance per server process (if you are using uwsgi or working in mod_wsgi, you will most likely have more than one processing request). Thus, the user can hit one endpoint of your application, pay the price for fetching data, and then at the next request turn to another process and pay that cost again (because although process A has data caching, Process B does not).

The only way around this is to use the storage outside the process (either the cache of the parent process, or a separate Python process that acts as a cache server or some persistence store [Redis, PostGres, etc.])

+4
source share

All Articles