【发布时间】:2017-09-27 01:13:13
【问题描述】:
我有一个在 python 中使用 Tornado 框架的基本 websocket 服务器。我想在服务器函数之间共享一个值:
class EchoWebSocket(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print ("connection opened")
def on_close(self):
tornado.ioloop.IOLoop.instance().stop()
print ("connection closed")
def on_message(self,message):
print (message)
def Main():
application = tornado.web.Application([(r"/", EchoWebSocket),])
application.listen(9000)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
Main()
我试图从这样的类中创建一个全局对象:
class operate:
state = "mutual"
def __init__(self):
self.state = 'mutual'
def play(self):
self.state = 'play'
def pause(self):
self.state = 'pause'
def getStatus(self):
return self.state
并调用一个全局对象,猜测由于该对象是全局对象,所以不会在每条消息中都创建一个新对象:
def open(self):
global objectTV
objectTV = operate()
objectTV.play()
.
.
.
.
def on_message(self,message):
global objectTV
objectTV = operate()
print(objectTV.getStatus())
但它总是打印'mutual'?
【问题讨论】:
标签: python web-services tornado