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()