[issue42392] remove the 'loop' parameter from __init__ in all classes in asyncio.locks
Andrew Svetlov
report at bugs.python.org
Thu Nov 19 01:58:16 EST 2020
Andrew Svetlov <andrew.svetlov at gmail.com> added the comment:
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...
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42392>
_______________________________________
More information about the Python-bugs-list
mailing list