Last active
June 23, 2017 18:06
-
-
Save harlowja/5544d84e8e734ea1cc7c163eff007531 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
class Event(object): | |
def __init__(self): | |
self._cond = threading.Condition() | |
self._val = False | |
def set(self): | |
with self._cond: | |
self._val = True | |
self._cond.notify_all() | |
def clear(self): | |
with self._cond: | |
self._val = False | |
def is_set(self): | |
return self._val | |
def wait(self, timeout=None): | |
with self._cond: | |
if timeout is None: | |
self._cond.wait() | |
return self.is_set() | |
else: | |
started_at = time.time() | |
while (time.time() - started_at < timeout): | |
if self.is_set(): | |
break | |
else: | |
print("Still waiting...") | |
self._cond.wait(0.01) | |
return self.is_set() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment