title: asyncio.get_event_loop() fails when following asyncio.run()
body: <p>I'm trying to find a solution to <strong>call async function in a Synchronous context</strong>.</p>
  <p>And following is my references:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/55003789/python-call-callback-after-async-function-is-done">Python call callback after async function is done</a></li>
  <li><a href="https://stackoverflow.com/questions/27796294/when-using-asyncio-how-do-you-allow-all-running-tasks-to-finish-before-shutting">When using asyncio, how do you allow all running tasks to finish before shutting down the event loop</a></li>
  <li><a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html" rel="nofollow noreferrer">https://docs.python.org/zh-cn/3/library/asyncio-task.html</a></li>
  <li><a href="https://stackoverflow.com/questions/46827007/runtimeerror-this-event-loop-is-already-running-in-python">RuntimeError: This event loop is already running in python</a></li>
  <li><a href="https://stackoverflow.com/questions/56821223/call-async-function-in-main-function">call async function in main function</a></li>
  </ul>
  <p>But I find that, <strong>asyncio.get_event_loop() fails</strong> when following asyncio.run(), here is my code to reproduce this issue:</p>
  <pre><code>import asyncio
  
  async def asyncfunction(n):
      print(f'before sleep in asyncfunction({ n })')
      await asyncio.sleep(1)
      print(f'after sleep in asyncfunction({ n })')
      return f'result of asyncfunction({ n })'
  
  def callback(r):
      print(f'inside callback, got: {r}')
  
  r0 = asyncio.run(asyncfunction(0)) # cause following asyncio.get_event_loop() fail.
  callback(r0)
  print('sync code following asyncio.run(0)')
  
  r1 = asyncio.run(asyncfunction(1)) # but following asyncio.run() still works.
  callback(r1)
  print('sync code following asyncio.run(1)')
  
  async def wrapper(n):
      r = await asyncfunction(n)
      callback(r)
  
  asyncio.get_event_loop().create_task(wrapper(2)) #fail if there  is asyncio.run() before
  print('sync code following loop.create_task(2)')
  
  #RuntimeError: There is no current event loop in thread 'MainThread'.
  
  asyncio.get_event_loop().create_task(wrapper(3)) #the second call works if there is no asyncio.run() before
  print('sync code following loop.create_task(3)')
  
  # main
  
  _all = asyncio.gather(*asyncio.all_tasks(asyncio.get_event_loop()))
  asyncio.get_event_loop().run_until_complete(_all)
  </code></pre>
  <p>I think it might because that the event loop is &quot;consumed&quot; by something somehow, and  <code>asyncio.set_event_loop(asyncio.new_event_loop())</code> might be a workaround, but I'm not sure <strong>whether that is an expected usage for end-user to set event loop mannually</strong>. And I'm also wondering <strong>why</strong> and <strong>how everything happens</strong> here.</p>
  <hr />
  <p>After read some of the source code of <a href="https://sphinx-themes.org/sample-sites/default-classic/_modules/asyncio/runners/" rel="nofollow noreferrer">asyncio.run</a>. I can know why this happens.</p>
  <p>But I'm still wondering <strong>what is the expected way to call async function in a Synchronous context</strong> ?</p>
  <p>It seems that the following code works (set a new event loop after each <code>asyncio.run()</code> call) :</p>
  <pre><code>asyncio.run(asyncfunction()) 
  asyncio.set_event_loop(asyncio.new_event_loop())
  </code></pre>
  <p>but that is somehow weird, and doesn't seems to be the expected way.</p>