Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 414918a

Browse files
committed
use the with statement for locking the internal condition (closes #25362)
Patch by Nir Soffer.
1 parent b397e3b commit 414918a

1 file changed

Lines changed: 3 additions & 12 deletions

File tree

Lib/threading.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,9 @@ def set(self):
511511
that call wait() once the flag is true will not block at all.
512512
513513
"""
514-
self._cond.acquire()
515-
try:
514+
with self._cond:
516515
self._flag = True
517516
self._cond.notify_all()
518-
finally:
519-
self._cond.release()
520517

521518
def clear(self):
522519
"""Reset the internal flag to false.
@@ -525,11 +522,8 @@ def clear(self):
525522
set the internal flag to true again.
526523
527524
"""
528-
self._cond.acquire()
529-
try:
525+
with self._cond:
530526
self._flag = False
531-
finally:
532-
self._cond.release()
533527

534528
def wait(self, timeout=None):
535529
"""Block until the internal flag is true.
@@ -546,14 +540,11 @@ def wait(self, timeout=None):
546540
True except if a timeout is given and the operation times out.
547541
548542
"""
549-
self._cond.acquire()
550-
try:
543+
with self._cond:
551544
signaled = self._flag
552545
if not signaled:
553546
signaled = self._cond.wait(timeout)
554547
return signaled
555-
finally:
556-
self._cond.release()
557548

558549

559550
# A barrier class. Inspired in part by the pthread_barrier_* api and

0 commit comments

Comments
 (0)