title: Simple Python multi threaded webserver with Asyncio and events called in main function
body: <p>I have a simple Python program that I want to do three things:</p>
  <ul>
  <li>Serve an HTTP document</li>
  <li>Serve Websockets</li>
  <li>Interact with the Websocket data</li>
  </ul>
  <p>I am trying to use / grok <code>asyncio</code>.  The issue is that I can't figure out how to access data acquired from a function in the main event loop.</p>
  <p>For example in my code below I have two threads.</p>
  <p>One thread is the HTTP server thread, one thread is the Websocket server thread and there is the main thread.</p>
  <p>What I want to do is to print data captured in the websocket receiving thread in the main thread.</p>
  <p>The only way I know how to do this is to use Queues to pass data between threads at which point I do not even know what the advantage of using <code>asyncio</code> is.</p>
  <p>Similarly, it feels weird to pass the event loop to the <code>serve_websocket</code> function.</p>
  <p>Can anyone please explain how to architect this to get data from the Websocket function into the main function?</p>
  <p>It seems like / I want a way to do this without using the <code>threading</code> library at all, which seems possible. In an async project I would want to react to websocket events in different function than where they are called.</p>
  <p>NOTE:  I know there are other libraries for websockets and http serving <em>with</em> asyncio but this is an example to help me understarnd how to structure projects using this paradigm.</p>
  <p>Thanks</p>
  <pre><code>#!/usr/bin/env python
  
  import json
  import socketserver
  import threading
  import http.server
  import asyncio
  import time
  
  import websockets
  
  SERVER_ADDRESS = '127.0.0.1'
  HTTP_PORT = 8087
  WEBSOCKET_PORT = 5678
  
  
  def serve_http():
      http_handler = http.server.SimpleHTTPRequestHandler
      with socketserver.TCPServer((&quot;&quot;, HTTP_PORT), http_handler) as httpd:
          print(f'HTTP server listening on port {HTTP_PORT}')
          httpd.serve_forever()
  
  
  def serve_websocket(server, event_loop):
      print(f'Websocket server listening on port {WEBSOCKET_PORT}')
      event_loop.run_until_complete(server)
      event_loop.run_forever()
  
  
  async def ws_callback(websocket, path):
      while True:
          data = await websocket.recv()
          # How do I access parsed_data in the main function below
          parsed_data = json.loads(data)
          await websocket.send(data)
  
  
  def main():
      event_loop = asyncio.get_event_loop()
      ws_server = websockets.serve(ws_callback, SERVER_ADDRESS, WEBSOCKET_PORT)
  
      threading.Thread(target=serve_http, daemon=True).start()
      threading.Thread(target=serve_websocket, args=(ws_server, event_loop), daemon=True).start()
  
      try:
          while True:
              # Keep alive - this is where I want to access the data from ws_callback
              # i.e.
              # print(data.values)
              time.sleep(.01)
  
      except KeyboardInterrupt:
          print('Exit called')
  
  
  if __name__ == '__main__':
      main()
  </code></pre>