bpo-32314: Implement asyncio.run()#4852
Conversation
|
|
||
| loop = events.new_event_loop() | ||
| try: | ||
| events.set_event_loop(loop) |
There was a problem hiding this comment.
set_event_loop() cannot fail (it has a couple asserts but we can neglect this fact).
There was a problem hiding this comment.
Sure it can. It calls policy.set_event_loop(), and given the fact that users can provide their own buggy policies, we can't really say that set_event_loop is 100% safe.
There was a problem hiding this comment.
It's a flag of broken third party loop implementation, not user code problem.
Buggy loop can do weird things, I expect asyncio will be broken somehow anyway.
There was a problem hiding this comment.
What's your point?
The point of this code is to always close the loop, no matter what. What benefit is there in moving set_event_loop() one line up?
There was a problem hiding this comment.
Anyways, if you think that the code will read better if set_event_loop is outside of the try block I can move it. We call set_event_loop in the finally block along with the loop.close(), so as you say: if these things are broken then nothing will actually work anyways.
There was a problem hiding this comment.
Well, it is very minor thing.
From my perspective wrapping loop.run_until_complete(main) only increases code readability a little but I can live with current implementation too.
| loop = events.new_event_loop() | ||
| try: | ||
| events.set_event_loop(loop) | ||
| loop.set_debug(debug) |
There was a problem hiding this comment.
Again, it can, if, say, I have a bug in uvloop.Loop.set_debug method. I prefer to assume nothing, when it's possible to set custom policies and custom event loops.
Doc/library/asyncio-task.rst
Outdated
| @@ -128,17 +143,15 @@ using the :meth:`sleep` function:: | |||
| import datetime | |||
|
|
|||
| async def display_date(loop): | |||
There was a problem hiding this comment.
the loop argument should be removed now
| print(datetime.datetime.now()) | ||
| if (loop.time() + 1.0) >= end_time: | ||
| break | ||
| await asyncio.sleep(1) |
There was a problem hiding this comment.
this is a bit more concise
while (loop.time() + 1.0) < end_time:
print(datetime.datetime.now())
await asyncio.sleep(1)There was a problem hiding this comment.
We can replace this while loop with for _ in range(5) and the example will only become clearer.
We'll have a separate pass over asyncio docs before 3.7 is released. We'll try to come up with better examples and improve the current ones. So for now, I'd keep this snippet as is.
ilevkivskyi
left a comment
There was a problem hiding this comment.
Thanks! It will be great to have this in 3.7.
|
@asvetlov Andrew, I'll wait for your comments. |
|
@ilevkivskyi Thanks! If you have any ideas about any APIs that would be great to have in asyncio, please open an issue. I have some time to work on asyncio and we still have a timeframe to make things happen. |
https://bugs.python.org/issue32314