title: How to override a sync function to run async code
body: <p>I'm looking to define a new connection as shown here: <a href="https://udsoncan.readthedocs.io/en/latest/udsoncan/connection.html#defining-a-new-connection" rel="nofollow noreferrer">https://udsoncan.readthedocs.io/en/latest/udsoncan/connection.html#defining-a-new-connection</a></p>
  <p>However, I want to call async code from there (i.e. make the send/wait asynchronous). I can't seem to get it working.</p>
  <p>Consider the following example as what I am trying to achieve:</p>
  <pre><code>import asyncio
  
  async def some_task():
      await asyncio.sleep(1)  # Async task
      print(&quot;Done&quot;)
      
  def sync_method():
      loop = asyncio.get_event_loop()
      loop.run_until_complete(asyncio.sleep(5)) # Some async event
      print(&quot;sure&quot;)
  
  async def main():
      sync_method()
      await asyncio.gather(some_task(), some_task(), some_task())
      
  
  if __name__ == &quot;__main__&quot;:
      asyncio.run(main())
  </code></pre>
  <p>But this throws an error (sleep was never awaited; but if I await it then I get error of await called from non async function). But I read (see <a href="https://stackoverflow.com/a/51762340">here</a>) that this was how to call an async function from a sync function.</p>
  <p>So, essentially my code is asynchronous and <code>main</code> is run in the event loop. Now, I want to call the synchronous function but essentially want to make it non-blocking by using async methods.</p>
  <p>What am I doing wrong?</p>
  <p>Edit - traceback as requested...</p>
  <pre><code>Traceback (most recent call last):
    File &quot;.\tmp_sync_async.py&quot;, line 18, in &lt;module&gt;
      asyncio.run(main())
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\asyncio\runners.py&quot;, line 43, in run
      return loop.run_until_complete(main)
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py&quot;, line 616, in run_until_complete
      return future.result()
    File &quot;.\tmp_sync_async.py&quot;, line 13, in main
      sync_method()
    File &quot;.\tmp_sync_async.py&quot;, line 9, in sync_method
      loop.run_until_complete(asyncio.sleep(3))
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py&quot;, line 592, in run_until_complete
      self._check_running()
    File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py&quot;, line 552, in _check_running
      raise RuntimeError('This event loop is already running')
  RuntimeError: This event loop is already running
  sys:1: RuntimeWarning: coroutine 'sleep' was never awaited
  </code></pre>
  <p>Update:
  From what I found, the <code>run_until_complete</code> way is fine if starting with sync code and then trying to run async code (i.e. if <code>main()</code> in my case was sync code).</p>