title: How do I set the asyncio event loop for a thread in Python?
body: <p>I'm trying to create two threads that each have their own asyncio event loop.</p>
  
  <p>I've tried the following code but it doesn't seem to work:</p>
  
  <pre><code>import asyncio
  from threading import Thread
  
  def hello(thread_name):
      print('hello from thread {}!'.format(thread_name))
  
  event_loop_a = asyncio.new_event_loop()
  event_loop_b = asyncio.new_event_loop()
  
  def callback_a():
      asyncio.set_event_loop(event_loop_a)
      asyncio.get_event_loop().call_soon_threadsafe(lambda: hello('a'))
  
  def callback_b():
      asyncio.set_event_loop(event_loop_b)
      asyncio.get_event_loop().call_soon_threadsafe(lambda: hello('b'))
  
  thread_a = Thread(target=callback_a, daemon=True)
  thread_b = Thread(target=callback_b, daemon=True)
  thread_a.start()
  thread_b.start()
  </code></pre>
  
  <p>My use case is calling Tornado web framework's websocket_connect async function.</p>