Cache \ Session in Python

0

I have a script in Python that runs a Tornado application. Within onmessage I have several variables that will keep an equal value always, so there is no need to fill them whenever a new message arrives for the Tornado.

Currently I do this: save the variable as global and set a value to it; in this way the script is always running, the value persists.

I would like to know if there is any library that generates a cookie or session so I can save these variables.

Here's an example of what the code looks like now:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

var1 = ""
def teste():
    global var1
    if var1 == "":
        var 1 = "texto que vai persistir"
    print var1
    return var1

class WSMesa(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
    def on_message(self, message):
        retorno = teste()
        self.write_message(retorno)
    def on_close(self):
        print 'connection closed'
    def check_origin(self, origin):
        return True

application = tornado.web.Application([
    (r'/mesa', WSMesa)
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(9999)
    tornado.ioloop.IOLoop.instance().start()
    
asked by anonymous 19.12.2017 / 11:21

1 answer

0

Because you do not store in the class itself, you will be able to use an attribute that can be seen among all instances of the same class.

class WSMesa(tornado.websocket.WebSocketHandler):
    var1 = 'word' # atributo de classe

    def open(self):
        print 'new connection'

    def on_message(self, message):
        retorno = self.var1 #acessa atributo de classe
        self.write_message(retorno)

    def on_close(self):
        print 'connection closed'

    def check_origin(self, origin):
        return True

And everything else continues, just load the test function and the global variable var1

    
19.12.2017 / 19:45