Message381385
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. |
|
Date |
User |
Action |
Args |
2020-11-19 00:18:23 | aeros | set | recipients:
+ aeros, asvetlov, yselivanov |
2020-11-19 00:18:23 | aeros | set | messageid: <1605745103.43.0.012919722193.issue42392@roundup.psfhosted.org> |
2020-11-19 00:18:23 | aeros | link | issue42392 messages |
2020-11-19 00:18:23 | aeros | create | |
|