2
到目前为止,我一直通过将变量从一个Tornado
类声明为global
来传递变量。我认为这可能不是这样做的理想方式。这里是我的代码示例:在Tornado类中传递全局变量
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
我想知道,是否在这个框架中什么是共享变量的更合适的方式。
我只做了几个星期的蟒蛇和龙卷风,所以请原谅我的无知。
谢谢
感谢您的提醒。我现在改变了我的代码,所以客户机 - 服务器之间的唯一通信就是通过websockets。这也意味着我可以消除代码中的所有全局变量。 – user1869421
听起来不错,详细一点。如果你看看龙卷风聊天演示代码,他们会使用一个全局的“服务员”列表,这些列表都存在于其中一个类中。虽然https://github.com/facebook/tornado/blob/master/demos/chat/chatdemo.py取决于您的应用程序可能过度杀伤 – aychedee