Commits

Thomas Kluyver committed 70a4b07

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

Comments (0)

Files changed (2)

+syntax: glob
+*.pyc
+"""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
 START_INTERACTIVE = True
 
 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
                 pass
             time.sleep(self.interval)
 
-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()
 
 if START_INTERACTIVE:
     start()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.