Skip to content

Commit

Permalink
Merge pull request #116 from simonklb/listener-thread-exception-handler
Browse files Browse the repository at this point in the history
Add listener thread exception handler
  • Loading branch information
Half-Shot authored Apr 25, 2017
2 parents debc5a2 + 2411ec1 commit 3b094e6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,15 @@ def listen_for_events(self, timeout_ms=30000):
"""
self._sync(timeout_ms)

def listen_forever(self, timeout_ms=30000):
def listen_forever(self, timeout_ms=30000, exception_handler=None):
""" Keep listening for events forever.
Args:
timeout_ms (int): How long to poll the Home Server for before
retrying.
exception_handler (func(exception)): Optional exception handler
function which can be used to handle exceptions in the caller
thread.
"""
bad_sync_timeout = 5000
self.should_listen = True
Expand All @@ -346,16 +349,24 @@ def listen_forever(self, timeout_ms=30000):
raise e
except Exception as e:
logger.exception("Exception thrown during sync")
if exception_handler is not None:
exception_handler(e)
else:
raise

def start_listener_thread(self, timeout_ms=30000):
def start_listener_thread(self, timeout_ms=30000, exception_handler=None):
""" Start a listener thread to listen for events in the background.
Args:
timeout (int): How long to poll the Home Server for before
retrying.
exception_handler (func(exception)): Optional exception handler
function which can be used to handle exceptions in the caller
thread.
"""
try:
thread = Thread(target=self.listen_forever, args=(timeout_ms, ))
thread = Thread(target=self.listen_forever,
args=(timeout_ms, exception_handler))
thread.daemon = True
self.sync_thread = thread
self.should_listen = True
Expand Down

0 comments on commit 3b094e6

Please sign in to comment.