Message381389
Perhaps Kyle is right, I had a misunderstanding with `get_running_loop()` vs `_get_running_loop()`.
The last version seems good except for the rare chance of race condition.
The safe code can look like:
global_lock = threading.Lock() like GIL
def _get_loop(self):
loop = asyncio.get_running_loop()
if self._loop is None:
# the lock is required because
# the thread switch can happen
# between `self._loop is None` check
# and `self._loop = loop` assignment
with global_lock:
if self._loop is not None:
self._loop = loop
if loop is not self._loop: raise
The alternative is using the fast C atomic `compare_and_swap` function
which is executed under the hold GIL.
We need the pure-Python fallback anyway.
Multithreading is hard... |
|
Date |
User |
Action |
Args |
2020-11-19 06:58:16 | asvetlov | set | recipients:
+ asvetlov, yselivanov, aeros |
2020-11-19 06:58:16 | asvetlov | set | messageid: <1605769096.67.0.0249580434622.issue42392@roundup.psfhosted.org> |
2020-11-19 06:58:16 | asvetlov | link | issue42392 messages |
2020-11-19 06:58:16 | asvetlov | create | |
|