title: Threaded, non-blocking websocket client
body: <p>I am wanting to run a program in Python that sends a message every second via web sockets to a Tornado server. I have been using the example on websocket-client;</p>
  
  <p>This example does not work, because <code>ws.run_forever()</code> will stop the execution of the while loop.</p>
  
  <p>Can somebody give me an example of how to correctly implement this as a threaded class which I can both call the send method of, but also receive messages?</p>
  
  <pre><code>import websocket
  import thread
  import time
  
  def on_message(ws, message):
      print message
  
  def on_error(ws, error):
      print error
  
  def on_close(ws):
      print "### closed ###"
  
  def on_open(ws):
      pass
  
  if __name__ == "__main__":
      websocket.enableTrace(True)
      ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
      ws.on_open = on_open
      ws.run_forever()
  
      while True:
          #do other actions here... collect data etc.
          for i in range(100):
              time.sleep(1)
              ws.send("Hello %d" % i)
          time.sleep(1)
  </code></pre>