[Python-ideas] Make threading.Event support test-and-set
Celelibi
celelibi at gmail.com
Sat Nov 26 20:00:07 EST 2016
Hello,
I have a case of multithreaded program where under some condition, I
would want to test the flag of the Event object and clear it atomically.
This would allow me to know which thread has actually cleared the flag
and thus should perform some more work before setting it again.
The isSet method is of course not good enough because it's subject to
race conditions. (Not even sure the GIL would make it safe.)
The changes to threading.py would be pretty trivial:
@@ -515,8 +515,10 @@
"""
with self._cond:
+ prevflag = self._flag
self._flag = True
self._cond.notify_all()
+ return prevflag
def clear(self):
"""Reset the internal flag to false.
@@ -526,7 +528,9 @@
"""
with self._cond:
+ prevflag = self._flag
self._flag = False
+ return prevflag
def wait(self, timeout=None):
"""Block until the internal flag is true.
What do you think about it?
Best regards,
Celelibi
More information about the Python-ideas
mailing list