From 0a95f7cb654b498ad974174542c299839017f830 Mon Sep 17 00:00:00 2001 From: Ruan Kunliang Date: Wed, 22 Jul 2015 19:42:18 +0800 Subject: [PATCH] add callback in BgServingThread to get notice when the connection is closed by peer --- rpyc/utils/helpers.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rpyc/utils/helpers.py b/rpyc/utils/helpers.py index 75cedc9e..c57dd5aa 100644 --- a/rpyc/utils/helpers.py +++ b/rpyc/utils/helpers.py @@ -189,11 +189,12 @@ class BgServingThread(object): SERVE_INTERVAL = 0.0 SLEEP_INTERVAL = 0.1 - def __init__(self, conn): + def __init__(self, conn, callback=None): self._conn = conn self._thread = threading.Thread(target = self._bg_server) self._thread.setDaemon(True) self._active = True + self._callback = callback self._thread.start() def __del__(self): if self._active: @@ -205,7 +206,10 @@ def _bg_server(self): time.sleep(self.SLEEP_INTERVAL) # to reduce contention except Exception: if self._active: - raise + self._active = False + if self._callback is None: + raise + self._callback() def stop(self): """stop the server thread. once stopped, it cannot be resumed. you will have to create a new BgServingThread object later."""