23

I have seen running a blocking code using

loop = asyncio.get_running_loop()
await loop.run_in_executor(None, blockingfunc)

And

loop = asyncio.get_event_loop()
await loop.run_in_executor(None, blockingfunc)

When should we use asyncio.get_running_loop() vs asyncio.get_event_loop()?

1
  • 4
    According to the docs, get_running_loop will raise a RuntimeError is no loop is running; get_event_loop, on the other hand, will create one. Commented Dec 8, 2020 at 20:38

3 Answers 3

14

In accordance with the official documentation, both the get_running_loop and get_event_loop are used to actually get an active loop, with the difference that the latter get_event_loop has more complex behaviour, thus get_running_loop can be used widely in the applications, where the existance of the loop is not questionable.

Moreover, in the documentation it is suggested to used run method to obtain an event loop (available starting from the Python 3.7 version), e.g.

async def main():
    await asyncio.sleep(1)
    print('hello')

asyncio.run(main())   

instead of:

async def main():
    await asyncio.sleep(1)
    print('hello')
        
loop = asyncio.get_event_loop()
task = loop.create_task(main)
loop.run_forever()

Please note, that the run method solution can work weirdly in Jupyter Notebook.

Sign up to request clarification or add additional context in comments.

Comments

5

asyncio.get_event_loop() has the caracteristics of asyncio.get_running_loop(), asyncio.new_event_loop() and asyncio.set_event_loop().

asyncio.get_event_loop() deprecated since version 3.10:

Get the current event loop.

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.

asyncio.get_running_loop():

Return the running event loop in the current OS thread.

asyncio.new_event_loop():

Create and return a new event loop object.

asyncio.set_event_loop():

Set loop as a current event loop for the current OS thread

For example, if running this code below:

import asyncio

async def test():
    while True:
        print("Test")
        await asyncio.sleep(1)

async def call_test():
    loop = asyncio.get_running_loop()
    loop.create_task(test())

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

loop.create_task(call_test())
loop.run_forever()

Test is printed every second:

Test
Test
Test
...

So now, we can replace asyncio.get_running_loop() with asyncio.get_event_loop() in call_test() and replace loop = asyncio.new_event_loop() and asyncio.set_event_loop(loop) with asyncio.get_event_loop() as shown below:

import asyncio

async def test():
    while True:
        print("Test")
        await asyncio.sleep(1)

async def call_test():
    loop = asyncio.get_event_loop() # Here
    # loop = asyncio.get_running_loop()
    loop.create_task(test())

loop = asyncio.get_event_loop() # Here
# loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop)

loop.create_task(call_test())
loop.run_forever()

Then, Test is still printed every second:

Test
Test
Test
...

So, about when to use asyncio.get_event_loop() or asyncio.get_running_loop(), you shouldn't use asyncio.get_event_loop() anytime because it is deprecated since version 3.10. So instead, you should use asyncio.get_running_loop() everytime and also asyncio.new_event_loop() and asyncio.set_event_loop(). But if you really want to use asyncio.get_event_loop(), use it to decrease code.

1 Comment

You state that get_event_loop is deprecated since Python 3.10, and I have seen that in a blog post as well, but that's actually not true. The function itself is not deprecated, but it will log a deprecation warning in certain scenarios, when no loop is running. It's actually still necessary to use this, instead of get_running_loop, in cases outside of a co-routine or callback, since you are never in a running loop. From there, get_running_loop will always raise an error, but get_event_loop will work correctly.
1

According to the asyncio documentation, get_event_loop is deprecated since 3.12. The get_running_loop function is recommended because it has a more predictable output. However, get_running_loop only works if there is a running loop, and must be called from a coroutine or a callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.