Skip to content

Instantly share code, notes, and snippets.

@harlowja
Last active June 23, 2017 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlowja/5544d84e8e734ea1cc7c163eff007531 to your computer and use it in GitHub Desktop.
Save harlowja/5544d84e8e734ea1cc7c163eff007531 to your computer and use it in GitHub Desktop.
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