Python Tornado updating shared data between requests

后端 未结 2 1105
半阙折子戏
半阙折子戏 2021-02-09 03:23

I have a Python Tornado app. The app contains request handlers, for which I am passing data to like (the code below is not complete, and is just to illustrate what I want):

2条回答
  •  北海茫月
    2021-02-09 03:49

    Another strategy, in addition to what dano mentions above is to attach the shared data to the Application object.

    class MyApplication(tornado.web.Application):
    
        def __init__(self):
    
           self.shared_attribute = foo;
    
            handlers = [#your handlers here]        
            settings = dict(#your application settings here)
            super().__init__(handlers, **settings)
    
    
    server = tornado.httpserver.HTTPServer(MyApplication())
    server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
    

    Next you can access shared_attribute defined above in all your request handlers using self.application.shared_attribute.

    You update it at one place and it immediately reflects in all your subsequent calls to the request handlers.

提交回复
热议问题