Итак, у меня есть 2 файла Python, main.py, который является основным файлом, и web.py, который содержит код веб-приложения (с использованием Tornado).Поэтому в web.py я хочу поделиться переменными между основным классом веб-приложения (WebAppModule, который создается в main.py) и другим классом (WSHandler, который обрабатывает соединение WebSocket со стороны клиента).Однако единственный способ, которым я мог придумать, - это использовать глобальную переменную (как показано ниже через переменную is_button_clicked ).Есть ли другой способ сделать это?
main.py :
from web import WebAppModule
web_client = WebAppModule(9000)
web.py :
clients = []
wsThread = None
is_button_clicked = None
class WebAppModule():
def __init__(self,portnumber):
global is_button_clicked
is_button_clicked = False
self.process_running = ""
self.connection_open = False
def some_other_function:
pass
class MyStaticFileHandler(tornado.web.StaticFileHandler):
def set_extra_headers(self, path):
self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('web/index.html')
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
if len(clients)==0:
clients.append(self)
print 'web app connection opened...'
else:
print 'Another client already accessed the UI. Connection blocked...'
def check_origin(self, origin):
return True
def on_message(self, message):
for client in clients:
if "BUTTON_CLICKED" in message:
global is_button_clicked
is_button_clicked = True
def on_close(self):
try:
clients.remove(self)
print 'connection closed...'
except Exception, e:
print "Client not established. Skipped..."
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "web"),
"static_hash_cache": False,
}
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
], static_path=os.path.join(os.path.dirname(__file__), "web"), static_handler_class=MyStaticFileHandler)