title: Run and wait for asynchronous function from a synchronous one using Python asyncio
body: <p>In my code I have a class with properties, that occasionally need to run asynchronous code. Sometimes I need to access the property from asynchronous function, sometimes from synchronous - that's why I don't want my properties to be asynchronous. Besides, I have an impression that asynchronous properties in general is a code smell. Correct me if I'm wrong.</p>
  
  <p>I have a problem with executing the asynchronous method from the synchronous property and blocking the further execution until the asynchronous method will finish. </p>
  
  <p>Here is a sample code:</p>
  
  <pre><code>import asyncio
  
  
  async def main():
      print('entering main')
      synchronous_property()
      print('exiting main')
  
  
  def synchronous_property():
      print('entering synchronous_property')
      loop = asyncio.get_event_loop()
      try:
          # this will raise an exception, so I catch it and ignore
          loop.run_until_complete(asynchronous())
      except RuntimeError:
          pass
      print('exiting synchronous_property')
  
  
  async def asynchronous():
      print('entering asynchronous')
      print('exiting asynchronous')
  
  
  asyncio.run(main())
  </code></pre>
  
  <p>Its output:</p>
  
  <pre><code>entering main
  entering synchronous_property
  exiting synchronous_property
  exiting main
  entering asynchronous
  exiting asynchronous
  </code></pre>
  
  <p>First, the <code>RuntimeError</code> capturing seems wrong, but if I won't do that, I'll get <code>RuntimeError: This event loop is already running</code> exception. </p>
  
  <p>Second, the <code>asynchronous()</code> function is executed last, after the synchronous one finish. I want to do some processing on the data set by asynchronous method so I need to wait for it to finish.
  If I'll add <code>await asyncio.sleep(0)</code> after calling <code>synchronous_property()</code>, it will call <code>asynchronous()</code> before <code>main()</code> finish, but it doesn't help me. I need to run <code>asynchronous()</code> before <code>synchronous_property()</code> finish. </p>
  
  <p>What am I missing? I'm running python 3.7.</p>