From 1e9194a85c59f0f96779810c74e4e19e4dd08580 Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Mon, 11 Feb 2019 15:33:07 +0100 Subject: [PATCH] Replace sleep by wait --- jupyter_client/channels.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jupyter_client/channels.py b/jupyter_client/channels.py index 8c4ebf3c4..51d4d11a8 100644 --- a/jupyter_client/channels.py +++ b/jupyter_client/channels.py @@ -7,7 +7,7 @@ import atexit import errno -from threading import Thread +from threading import Thread, Event import time import zmq @@ -73,6 +73,7 @@ def __init__(self, context=None, session=None, address=None): # running is False until `.start()` is called self._running = False + self._exit = Event() # don't start paused self._pause = False self.poller = zmq.Poller() @@ -138,7 +139,7 @@ def run(self): while self._running: if self._pause: # just sleep, and skip the rest of the loop - time.sleep(self.time_to_dead) + self._exit.wait(self.time_to_dead) continue since_last_heartbeat = 0.0 @@ -154,7 +155,7 @@ def run(self): # sleep the remainder of the cycle remainder = self.time_to_dead - (time.time() - request_time) if remainder > 0: - time.sleep(remainder) + self._exit.wait(remainder) continue else: # nothing was received within the time limit, signal heart failure @@ -183,6 +184,7 @@ def is_beating(self): def stop(self): """Stop the channel's event loop and join its thread.""" self._running = False + self._exit.set() self.join() self.close()