烧瓶:'会议'与'G'? [英] Flask: 'session' vs. 'g'?

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

问题描述

我试图理解 g session 之间在功能和用途上的差异。两者都是挂起会话数据的对象,对吗?如果是这样,那究竟是什么区别,哪一个应该在什么情况下使用? 不, g 不是挂起会话数据的对象。 g 数据不会在请求之间持续存在。

session 为您提供了一个存放每个特定浏览器数据的地方 。作为您的Flask应用程序的用户,使用特定的浏览器,返回更多的请求,会话数据在这些请求之间进行传输。



g另一方面,在一个请求周期内的代码库的不同部分之间共享数据。可以在 before_request 钩子中设置 g ,在 teardown_request 阶段,一旦请求完成并发送给客户端,清除 g


I'm trying to understand the differences in functionality and purpose between g and session. Both are objects to 'hang' session data on, am I right? If so, what exactly are the differences and which one should I use in what cases?

解决方案

No, g is not an object to hang session data on. g data is not persisted between requests.

session gives you a place to store data per specific browser. As a user of your Flask app, using a specific browser, returns for more requests, the session data is carried over across those requests.

g on the other hand is data shared between different parts of your code base within one request cycle. g can be set up during before_request hooks, is still available during the teardown_request phase and once the request is done and sent out to the client, g is cleared.

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

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