Pycket会话管理器不在龙卷风中工作

时间:2014-08-11 07:46:01

标签: python session tornado

我想使用Pycket会话管理器在会话中设置值。看一下代码:

session = SessionManager(self)
session['key'] = 'OMG'

之后在另一个处理程序中我使用了以下代码:

session = SessionManager(self)
self.write(str(session['key']))

它写None!我该怎么办?

注意:redis在我的项目上工作正常,这是我的龙卷风设置:

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        url_patterns,debug=True,
        cookie_secret="61oETz3455545gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
        xsrf_cookies= False,
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path= os.path.join(os.path.dirname(__file__), "static"),

        **{
                'pycket': {
                    'engine': 'redis',
                    'storage': {
                        'db_sessions': 10,
                        'db_notifications': 11,
                        'max_connections': 2 ** 31,
                        },
                    'cookies': {
                        'expires_days': 120,
                        # 'domain' : SharedConnections.SiteNameUrl[SharedConnections.SiteNameUrl.index(".")+1,-1],
                        'domain' : 'domain.com',

                        },
                    },
                }
    )

1 个答案:

答案 0 :(得分:2)

使用这种方式

session.set('key', 'OMG')

我建议使用pycket按照这种方式

import tornado.web
from pycket.session import SessionMixin
from pycket.notification import NotificationMixin


class BaseHandler(tornado.web.RequestHandler, SessionMixin, NotificationMixin):
    def __init__(self, application, request, **kwargs):
        super(BaseHandler, self).__init__(application, request, **kwargs)


class IndexHandler(BaseHandler):
    def get(self, *args, **kwargs):
        self.session.set('key', 'value')
        p = self.session.get('key')
        self.render('index.html')