Kyle Stanley <[email protected]> added the comment:
Regarding the example _get_loop():
```
def _get_loop(self):
loop = asyncio.get_running_loop()
if self._loop is None:
self._loop = loop
if loop is not self._loop: raise
if not loop.is_running(): raise
```
Would this be added to all asyncio primitives to be called anytime a reference
to the loop is needed within a coroutine?
Also, regarding the last line "if not loop.is_running(): raise" I'm not 100%
certain that I understand the purpose. Wouldn't it already raise a RuntimeError
from `asyncio.get_running_loop()` if the event loop wasn't running?
The only thing I can think of where it would have an effect is if somehow the
event loop was running at the start of `_get_loop()` and then the event loop
was stopped (e.g. a loop in an alternative thread was stopped by the main
thread while the alternative thread was in the middle of executing
`_get_loop()`). But to me, that seems to be enough of an edge case to simplify
it to the following:
```
def _get_loop(self):
loop = asyncio.get_running_loop()
if self._loop is None:
self._loop = loop
if loop is not self._loop: raise
```
(Unless you intended for the first line `loop = asyncio.get_running_loop()` to
instead be using the private `asyncio._get_running_loop()`, which returns None
and doesn't raise. In that case, the original would be good to me.)
Other than that, I think the approach seems solid.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42392>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com