金字塔会话对象在约 20 分钟后消失

Pyramid session object disappears after ~20 mins

request.session中设置一个变量
request.session['display_name'] = 'foo'

然后在我的应用程序中显示

request.session.display_name

这在整个应用程序中都运行良好。但是,如果单独放置大约 20 分钟,我就不能再调用它了。我得到:

AttributeError: 'CookieSession' object has no attribute 'display_name'

阅读金字塔 sessions 应该如何工作,它应该在浏览器范围内仍然存在。

知道为什么它会在一段时间后被删除吗?

参见 timeout 的定义:

A number of seconds of inactivity before a session times out. If None then the cookie never expires. This lifetime only applies to the value within the cookie. Meaning that if the cookie expires due to a lower max_age, then this setting has no effect. Default: 1200.

假设您没有设置max_age,或者如果您将max_age设置为1200或更大,那么默认设置timeout(1200秒)将使cookie过期在那个经过的持续时间。

1200 秒/每分钟 60 秒 = 20 分钟

...这与您的经验一致。

尝试将 timeout 设置为所需的持续时间(以秒为单位),或设置为 None 以防止 cookie 在浏览器打开时过期。