Use a timeout when worker threads are polling

This commit is contained in:
Lonami Exo 2017-09-30 11:21:07 +02:00
parent 7cef5885fa
commit a3ae56ca9e

View File

@ -10,6 +10,8 @@ class UpdateState:
"""Used to hold the current state of processed updates. """Used to hold the current state of processed updates.
To retrieve an update, .poll() should be called. To retrieve an update, .poll() should be called.
""" """
WORKER_POLL_TIMEOUT = 5.0 # Avoid waiting forever on the workers
def __init__(self, workers=None): def __init__(self, workers=None):
""" """
:param workers: This integer parameter has three possible cases: :param workers: This integer parameter has three possible cases:
@ -36,9 +38,14 @@ class UpdateState:
"""Returns True if a call to .poll() won't lock""" """Returns True if a call to .poll() won't lock"""
return self._updates_available.is_set() return self._updates_available.is_set()
def poll(self): def poll(self, timeout=None):
"""Polls an update or blocks until an update object is available""" """Polls an update or blocks until an update object is available.
self._updates_available.wait() If 'timeout is not None', it should be a floating point value,
and the method will 'return None' if waiting times out.
"""
if not self._updates_available.wait(timeout=timeout):
return
with self._updates_lock: with self._updates_lock:
if not self._updates_available.is_set(): if not self._updates_available.is_set():
return return
@ -96,10 +103,11 @@ class UpdateState:
def _worker_loop(self, wid): def _worker_loop(self, wid):
while True: while True:
try: try:
update = self.poll() update = self.poll(timeout=UpdateState.WORKER_POLL_TIMEOUT)
# TODO Maybe people can add different handlers per update type # TODO Maybe people can add different handlers per update type
for handler in self.handlers: if update:
handler(update) for handler in self.handlers:
handler(update)
except StopIteration: except StopIteration:
break break
except Exception as e: except Exception as e: