title: Properly catching syntax errors with coroutines in Python
body: <p>I'm trying to write a little Python script that basically runs two things in parallel. I'm coming from JavaScript and trying to use Python's async/await coroutine features, but I'm having a little trouble getting it to work as I expect.</p>
  <p>It was easy enough to get a first two functions running in parallel. The problems I'm running into are relating to catching syntax errors.</p>
  <p>Specifically, I was trying to add some math to one of my functions but Python would just seem to hang on that step (prints before worked, after they did not). I traced my issues down to the syntax exception not bubbling up as I expect, specifically that the asyncio seems to hide errors if they happen inside &quot;futures&quot;. I think this is where my problem lies but I've had a heck of a time trying to catch the errors as I expect.</p>
  <p>I tried wrapping the whole offending coroutine in a <code>try</code>/<code>except</code> but that seemed to break my other coroutines (seemingly breaking my assumptions about how coroutines work).</p>
  <pre><code>#!/usr/bin/env python3
  
  import asyncio
  
  async def other():
      while True:
          await asyncio.sleep(1)
          print('foobar')
          
  async def test():
      try:
          while True:
              a = undefinedVariable
      except:
          print(&quot;Why doesn't this always catch??&quot;)
  
  # catches as expected
  asyncio.get_event_loop().run_until_complete(asyncio.wait([test()]))
  
  # doesn't catch, fills screen with &quot;foobar&quot; lines, once a second, runs forever
  asyncio.get_event_loop().run_until_complete(asyncio.wait([test(), other()]))
  
  # Trying other things...
  # Error:
  # RuntimeError: no running event loop
  # sys:1: RuntimeWarning: coroutine 'test' was never awaited&quot;
  asyncio.get_event_loop().run_until_complete(asyncio.wait([asyncio.create_task(t()) for t in [test, other]]))
  </code></pre>
  <p>Running a single problematic coroutine works as expected.</p>
  <p>Combining it with another coroutine that doesn't error makes the other coroutine stop respecting the <code>asyncio.sleep</code>.</p>
  <p>I once got to a point where if I hit Ctrl+C, it would stop printing &quot;foobar&quot;, print the syntax error, and then exit, but I failed to capture that code and it's lost to undoes.</p>
  <p>I feel like I'm missing something simple. Any pointers would be greatly appreciated.</p>