Skip to content

Instantly share code, notes, and snippets.

Last active June 9, 2023 19:37
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Show a demo the differences between Python `asyncio.get_event_loop()` and (vs) `asyncio.get_running_loop()`. And also explain the differences between `` and `loop.run_until_complete()`
import asyncio
# Ref:
# > If there is no current event loop set in the current OS thread,
# > the OS thread is main, and set_event_loop() has not yet been called,
# > asyncio will create a new event loop and set it as the current one
loop = asyncio.get_event_loop()
# So, loop2 is loop
loop2 = asyncio.get_event_loop()
print('loop is loop2: ', loop is loop2)
async def main(
desc: str
if asyncio.get_event_loop() is loop:
print(desc, ': match')
print(desc, ': not match')
# Ref:
# > This function always creates a new event loop and closes it at the end.
# > It should be used as a main entry point for asyncio programs,
# > and should ideally only be called once., ''))
# The output is:
# $ : not match
# Because `` does following things:
# - create a new event loop
# - set the newly-created event loop as the current event loop
# - run the coroutine util completed
# - set the current event loop as None
# ```python
# new_loop = asyncio.new_event_loop()
# asyncio.set_event_loop(new_loop)
# new_loop.run_until_complete(coro)
# asyncio.set_event_loop(None)
# loop.close()
# ```
except Exception as e:
print('asyncio.get_event_loop() exception after asyncio:', e)
# Why?
loop.run_until_complete(main(loop, 'loop.run_until_complete'))
# The output is
# $ loop.run_until_complete : match
# As explained above, we need to set the current event loop explicitly.
main(loop, 'asyncio.get_event_loop().run_until_complete')
# The output is
# $ asyncio.get_event_loop().run_until_complete : match
# Conclusion
# - never use `` in frameworks, or you might break the code of others
# - be careful by using ``, or it is recommended that you manage the event loop your own
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment