Kaydet (Commit) 5f32b236 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

use the with statement for locking the internal condition (closes #25362)

Patch by Nir Soffer.
üst 4ed35fc4
...@@ -580,12 +580,9 @@ class _Event(_Verbose): ...@@ -580,12 +580,9 @@ class _Event(_Verbose):
that call wait() once the flag is true will not block at all. that call wait() once the flag is true will not block at all.
""" """
self.__cond.acquire() with self.__cond:
try:
self.__flag = True self.__flag = True
self.__cond.notify_all() self.__cond.notify_all()
finally:
self.__cond.release()
def clear(self): def clear(self):
"""Reset the internal flag to false. """Reset the internal flag to false.
...@@ -594,11 +591,8 @@ class _Event(_Verbose): ...@@ -594,11 +591,8 @@ class _Event(_Verbose):
set the internal flag to true again. set the internal flag to true again.
""" """
self.__cond.acquire() with self.__cond:
try:
self.__flag = False self.__flag = False
finally:
self.__cond.release()
def wait(self, timeout=None): def wait(self, timeout=None):
"""Block until the internal flag is true. """Block until the internal flag is true.
...@@ -615,13 +609,10 @@ class _Event(_Verbose): ...@@ -615,13 +609,10 @@ class _Event(_Verbose):
True except if a timeout is given and the operation times out. True except if a timeout is given and the operation times out.
""" """
self.__cond.acquire() with self.__cond:
try:
if not self.__flag: if not self.__flag:
self.__cond.wait(timeout) self.__cond.wait(timeout)
return self.__flag return self.__flag
finally:
self.__cond.release()
# Helper to generate new thread names # Helper to generate new thread names
_counter = _count().next _counter = _count().next
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment