Reputation: 837
To date I have been passing variables from one Tornado
class to another by declaring them as global
. I think that this might not be the ideal way to do so. Here's a sample of my code:
class MHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.render('index.html')
def post(self):
global account_age
age = self.get_argument('age')
account_age = [age]
class AgeHandler(tornado.websocket.WebSocketHandler):
@tornado.web.asynchronous
@gen.engine
def open(self):
global account_age
print 'Your account is overdue by: ', account_age
I would like to know, whether in this framework what would be the more appropriate way to share variables.
I've only been doing python and Tornado for just a few weeks, so please excuse my ignorance.
Thanks
Upvotes: 2
Views: 2378
Reputation: 25569
Yeah, I wouldn't do that. Even in this simplified example it is clear that you have a race condition between the POST and the websocket opening. How can you guarantee that the person making the POST was the same person opening the websocket?
I usually hold onto a reference to each websocket connection in a global list/dict. Some kind of reference that lets me write the server side output to the correct connection.
Upvotes: 3