More gentle use of app.Yield in ProcessLogger
authorEdouard Tisserant
Thu, 29 Mar 2012 00:14:35 +0200
changeset 705 a5977f64d4d9
parent 704 5993b16fe2d0
child 710 d76f73c7ae5e
More gentle use of app.Yield in ProcessLogger
Beremiz.py
--- a/Beremiz.py	Wed Mar 28 21:08:31 2012 +0200
+++ b/Beremiz.py	Thu Mar 29 00:14:35 2012 +0200
@@ -284,7 +284,8 @@
             dc.DrawBitmap(self._bitmap, 0, 0, True)
 
                         
-from threading import Lock,Timer
+from threading import Lock,Timer,currentThread
+MainThread = currentThread().ident
 REFRESH_PERIOD = 0.1
 from time import time as gettime
 class LogPseudoFile:
@@ -299,6 +300,7 @@
         # to prevent rapid fire on rising log panel
         self.rising_timer = 0
         self.lock = Lock()
+        self.YieldLock = Lock()
         self.RefreshLock = Lock()
         self.stack = []
         self.LastRefreshTime = gettime()
@@ -313,11 +315,20 @@
                 self.LastRefreshTimer.cancel()
                 self.LastRefreshTimer=None
             if current_time - self.LastRefreshTime > REFRESH_PERIOD and self.RefreshLock.acquire(False):
-                wx.CallAfter(self._write)
+                self._should_write()
             else:
-                self.LastRefreshTimer = Timer(REFRESH_PERIOD, wx.CallAfter, [self._write])
+                self.LastRefreshTimer = Timer(REFRESH_PERIOD, self._should_write)
                 self.LastRefreshTimer.start()
 
+    def _should_write(self):
+        wx.CallAfter(self._write)
+        if MainThread == currentThread().ident:
+            app = wx.GetApp()
+            if app is not None:
+                if self.YieldLock.acquire(0):
+                    app.Yield()
+                    self.YieldLock.release()
+
     def _write(self):
         if self.output :
             self.output.Freeze();