Skip to content

Commit

Permalink
Fix ClientEvent timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 17, 2020
1 parent 5f2eaa2 commit f84f059
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions labthings/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ def wait(self, timeout: int = 5):
self.events[ident] = [Event(), time.time()]

# We have to reimplement event waiting here as we need native thread events to allow gevent context switching
wait_start = time.time()
while not self.events[ident][0].is_set():
gevent.time.sleep(0)
now = time.time()
if now - wait_start > timeout:
return False
gevent.sleep(0)
return True

def set(self):
def set(self, timeout=5):
"""Signal that a new frame is available."""
now = time.time()
remove = None
Expand All @@ -47,9 +51,9 @@ def set(self):
else:
# if the client's event is already set, it means the client
# did not process a previous frame
# if the event stays set for more than 5 seconds, then assume
# if the event stays set for more than `timeout` seconds, then assume
# the client is gone and remove it
if now - event[1] > 5:
if now - event[1] >= timeout:
remove = ident
if remove:
del self.events[remove]
Expand All @@ -60,4 +64,6 @@ def clear(self):
if ident not in self.events:
logging.error(f"Mismatched ident. Current: {ident}, available:")
logging.error(self.events.keys())
return False
self.events[id(getcurrent())][0].clear()
return True

0 comments on commit f84f059

Please sign in to comment.