title: How to run functions outside websocket loop in python (tornado)
body: <p>I'm trying to set up a small example of a public Twitter stream over websockets.  This is my websocket.py, and it's working.</p>
  
  <p>What I'm wondering is: how can I interact with the websocket from 'outside' the class WSHandler (ie. not only answer when receiving a message from websocket.js)?  Say I want to run some other function within this same script that would post "hello!" every five seconds and send that to the websocket (browser) without any interaction from client-side. How could I do that?</p>
  
  <p>So it's kind of a fundamental beginner's question, I suppose, about how to deal with classes as those below. Any pointers in any direction would be greatly appreciated!  </p>
  
  <pre><code>import os.path
  import tornado.httpserver
  import tornado.websocket
  import tornado.ioloop
  import tornado.web
  
  # websocket
  class FaviconHandler(tornado.web.RequestHandler):
      def get(self):
          self.redirect('/static/favicon.ico')
  
  class WebHandler(tornado.web.RequestHandler):
      def get(self):
          self.render("websockets.html")
  
  class WSHandler(tornado.websocket.WebSocketHandler):
      def open(self):
          print 'new connection'
          self.write_message("Hi, client: connection is made ...")
  
      def on_message(self, message):
          print 'message received: \"%s\"' % message
          self.write_message("Echo: \"" + message + "\"")
          if (message == "green"):
              self.write_message("green!")
  
      def on_close(self):
          print 'connection closed'
  
  
  
  handlers = [
      (r"/favicon.ico", FaviconHandler),
      (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
      (r'/', WebHandler),
      (r'/ws', WSHandler),
  ]
  
  settings = dict(
      template_path=os.path.join(os.path.dirname(__file__), "static"),
  )
  
  application = tornado.web.Application(handlers, **settings)
  
  if __name__ == "__main__":
      http_server = tornado.httpserver.HTTPServer(application)
      http_server.listen(8888)
      tornado.ioloop.IOLoop.instance().start()
  </code></pre>