-
-
Notifications
You must be signed in to change notification settings - Fork 178
Add more highlights in docs and examples that coroutines definitions always depend on loop context #332
Comments
Hi, On the line :
The loop is implicitly retrieved from asyncio.get_event_loop(). However, when calling asyncio.new_event_loop(), it doesn't set the default If you want to use several loops, one can be the default loop returned by Also, when one awaits on a future/task which is not bound to the loop |
Hi, @Martiusweb! Yes, I totally understand now, what happens and usually write my code against explicitly specified event loop. But I came to that after one year of development with asyncio :) And now I can say, it's one of the most unclear and tricky parts. And so I find introductory docs confusing about that. And regarding code examples:
But we have also defined loop as argument:
Why we don't pass it as an argument to
Yes, I didn't think about that, but sounds reasonable. Now it's just silently pending forever. While writing coroutine we should pick one of two options:
The first option is more verbose and more explicit. The second one is compact and implicit. And that's not really simple question, which option if preferable in a particular case. I think that reusable library should always pick the first option. And users (application) code may want to pick the second one. And, probably the most common misuse is forgetting to set current event loop. For example, as far as I remember test suit in Tornado framework creates new event loop for every test (its own, not asyncio one), and it's user's responsibility to set up corresponding current asyncio loop. |
Indeed, reusable libraries should follow the convention of an optional loop argument and defaulting to In the application code, I can't see cases where you want to use several loops in one thread. I chose explicit loop passing most of the time, but one could use a custom |
I think it would be good to have such explanation as introduction to coroutines / tasks with code samples for both approaches :) Currently, "Tasks and coroutines" documentation tells how to do things, but doesn't cover motivation and proper design practices. And I would insist that the first example is broken :) If we pass loop as argument, but do not use it, it's probably a bug. Just imagine, that |
I agree: those examples are a bit misleading, and the documentation misses a general discussion about how and when asyncio should be used. I remember that the PEP (https://www.python.org/dev/peps/pep-3156/) helped me a bit. |
read more here python/asyncio#312 and here python/asyncio#332
Hello, everyone! This is more discussion issue than bug report or kind of.
For my opinion code examples in official asyncio docs are broken. Well, not totally broken of course, but they give a wrong idea that coroutines definition are independent of loop context!
I'd stress the word definition. Because I think it's clear, that coroutines execution depends on loop. But it's totally unclear, that definition also depends on it.
Here's an example from docs:
https://docs.python.org/3/library/asyncio-task.html#example-coroutine-displaying-the-current-date
It works.
Now let's change a single line:
It doesn't.
But the fix isn't really hard:
Well, the next example is even worse:
It can't run on a custom loop. Because
compute()
andprint_sum()
coroutines have no idea of event loop and thus can't be fixed without changing interface.Well, I think we should handle that really carefully :)
Either rewrite examples in docs to provide correct patterns or handle custom loops automatically. About second option, don't know if that's possible or really good idea :)
The text was updated successfully, but these errors were encountered: