Thomas Kluyver committed 70a4b07

Avoid problems when calling start() or stop() several times in a row.

  • Participants
  • Parent commits daad5c4

Comments (0)

Files changed (2)

+syntax: glob
+"""This module runs continuous updates for R, such as redrawing graphs when
+the plot window is resized. Use the start() and stop() functions to turn
+updates on and off.
+Importing the module currently starts updates.
 from rpy2.rinterface import process_revents
 import time
 import threading
+# If this is True, start updates when the module is imported
 class _ReventThread(threading.Thread):
+    """The thread for processing R events. Use the module's start() and stop()
+    functions to switch it on and off. If you interact with this class
+    directly, it's up to you to ensure you don't start several of them."""
     daemon = True
-    def __init__(self, interval):
+    def __init__(self):
         super(_ReventThread, self).__init__()
         self.running = True
-        self.interval = interval
+        self.interval = 0.2
     def stop(self):
         self.running = False
-def start(interval = 0.2):
-    global eventthread
-    eventthread = _ReventThread(interval)
-    eventthread.start()
+_eventthread = _ReventThread()
+def start(interval=0.2):
+    """Start running continuous updates of plots etc. Specifying interval
+    defines how often it will update. The default is every 0.2 seconds.
+    If updates are already running, this will just change the interval."""
+    global _eventthread
+    _eventthread.interval = interval
+    if _eventthread.is_alive():
+        return
+    _eventthread.start()
 def stop():
-    eventthread.stop()
+    """Stop continuous updates. If updates aren't running, this will do
+    nothing."""
+    global _eventthread
+    if not _eventthread.is_alive():
+        return
+    _eventthread.stop()
+    # Create a new thread so it can be started again
+    _eventthread = _ReventThread()