烧瓶:会话的最大大小太小 [英] Flask: session max size too small

查看:81
本文介绍了烧瓶:会话的最大大小太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个数据分析Flask应用程序,该应用程序需要大量用户输入,进行一些计算,然后将结果投影到各个网页上。我正在使用pandas Dataframe来存储输入并执行计算。然后我将DF转换为字典并将其存储在会话对象中。

I'm building a data analysis Flask application which takes a ton of user inputs, performs some calculations then projects the results to various web pages. I'm using a pandas Dataframe to store the inputs and perform the calculations. Then I convert the DF to a dictionary and store that in the session object.

由于会话对象只能容纳约4k个字节,因此我遇到了问题。几个页面读取数据,因此我需要一种方法将一个请求中的大量数据(〜5k-50k)传递给另一个请求(会话对象可以很好地完成此操作,但对于较小的内存大小)。

I'm having problems due to the fact that the session object can only hold ~4k bytes. Several pages read the data so I need a means to pass this large amount of data (~5k-50k) from one request to another (which the session object does perfectly but for smaller memory sizes).

我可以为会话对象设置更高的存储限制吗(我想我不能,因为4k是cookie的限制,而会话对象是cookie)?还是我在这里还要做其他事情(将字典存储在数据库中,等等)?

Can I set the storage limit higher for the session object (I imagine I can't as 4k is the limit for a cookie and the session object is a cookie)? Or is there something else I should do here (store the dict in a database, etc.)?

编辑:

我在考虑一种可行的替代方法是从数据库(在我的情况下为mongodb)中获取数据,将其存储在本地变量中,然后将该变量直接传递给模板。是否有负面影响?可以将目录传递给模板的内存量是否有限制?参见下面的示例:

I'm thinking a viable alternative would be to grab the data from the database (mongodb in my case), store it in a local variable and pass that variable to the template directly. Are there negatives to this? And is there a limit on how much memory I can pass directory to a template? See example below:

@app.route('/results')
def results():
    # get data I need from database (~5k-50k bytes)
    data = mongo.db[collection_name].find_one({'key': 'query'})
    # pass directory to template (instead of storing in session object)
    return render_template('results_page.html', data=data)


推荐答案

是的,这听起来确实像是服务器端会话的案例。

这里有官方网站上的代码段,用于最受欢迎的数据库。

Yeah this definitely sounds like a case for server-side sessions.
There are code snippets on the official site for the most popular databases.

这些应该很容易迁移,因为它们使用与cookie会话系统相同的 SessionMixin 接口。

These shouldn't be hard to migrate to since they use the same SessionMixin interface as the cookie session system.

更简单的方法可能是使用 Flask- KVSession 声称

An even easier approach could be to use Flask-KVSession, which claims that


与Flask的集成是无缝的,一旦为Flask应用程序加载了扩展名,它将透明地替换Flask自己的会话管理。任何与会话一起使用的应用程序都应与Flask-KVSession相同

Integration with Flask is seamless, once the extension is loaded for a Flask application, it transparently replaces Flask’s own Session management. Any application working with sessions should work the same with Flask-KVSession

这篇关于烧瓶:会话的最大大小太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆