24

I'm using asyncio in my application and i'm a litte bit confused about passing the event loop as argument.

You've three possibilities when writing a function/method using the event loop:

  • Pass the asyncio event loop as argument
  • Don't use an argument for the event loop and use asyncio.get_event_loop()
  • Make it optional to pass the event loop as argument. If it is not passed, use asyncio.get_event_loop()

It seems that the last case is used most of the time but even in the asyncio api the usage is inconsistent. As I don't indent to use two seperated event loops what speaks against just using asyncio.get_event_loop() where needed?

What's the best way to go?

8
  • Can you please elaborate on "passing the event loop as argument"? Passing the event loop from where to where? This is a bit application specific rather than a generic question (depending on how the application is using the event loop the answer might vary). Commented Oct 31, 2016 at 14:04
  • asyncio.get_event_loop() returns an event loop, you can change it with asyncio.set_event_loop(). This will get/set the event loop on the _event_loop_policy, a global variable in the asyncio module. This is an easy way to share the eventloop in the application without a need to pass it around. So if you want to avoid having 2 separate event loops around, you may register the one event loop you've got in this global variable by set_event_loop(). Commented Oct 31, 2016 at 14:10
  • @farzad: Typically my application starts with loop=asyncio.get_event_loop(). Then I'm setting up the asynchronous stuff (coroutines, async servers, ...) and my own functions where I've got this question about passing the loop argument. When everything is set up, I run loop.run_forever(). Commented Oct 31, 2016 at 14:25
  • 2
    Related: asyncio-doc#13 Commented Oct 31, 2016 at 15:25
  • This is a great question, hope some more light can be shed on this by someone in the know... Commented Nov 5, 2016 at 12:32

1 Answer 1

12

A good (as in praised by Guido van Rossum) blog post discussing this is Some thoughts on asynchronous API design in a post-async/await world. With a follow up discussion from python core developers here.

TLDR;

If you're only using one event loop, it doesn't matter.

If you're managing multiple loops, and have python >= 3.6 it mostly doesn't matter: Do not use argument and use asyncio.get_event_loop() where needed, it will give you the correct loop.

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

2 Comments

It took me a while to verify that Guido was onboard with this. This message was useful for that: groups.google.com/d/msg/python-tulip/yF9C-rFpiKk/tk5oA3GLHAAJ
@JHS It looks like it's only deprecated to call get_event_loop to get a new event loop when there isn't already one running, bringing it in line with get_running_loop.

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.