[IPython-dev] [Enthought-Dev] [matplotlib-devel] Ctypes based prototype of PyOS_InputHook for wx 2.8 and 2.9

Ondrej Certik ondrej at certik.cz
Fri Jul 17 19:15:20 EDT 2009

On Fri, Jul 17, 2009 at 3:57 PM, Ville M. Vainio<vivainio at gmail.com> wrote:
> On Sat, Jul 18, 2009 at 12:54 AM, Brian Granger<ellisonbg.net at gmail.com> wrote:
>> best.  Bottom line = we are into a position of compromise because of wx.
>> The good news is that I think we can offer users a very flexible way of
>> tuning all these things.
> Perhaps adaptive autotuning algorithm could help your case; if stdin
> came in rapidly, poll again very soon, otherwise adjust the delay.

The following patch implements this in the #3 approach:

$ diff -Naur /home/ondrej/Desktop/inputhook.py inputhook.py
--- /home/ondrej/Desktop/inputhook.py	2009-07-17 14:09:34.000000000 -0600
+++ inputhook.py	2009-07-17 17:12:37.000000000 -0600
@@ -110,17 +110,26 @@
     This sleep time should be tuned though for best performance.
     import wx
+    from timeit import default_timer as clock
     app = wx.GetApp()
     if app is not None:
         assert wx.Thread_IsMain()

         evtloop = wx.EventLoop()
         ea = wx.EventLoopActivator(evtloop)
+        t = clock()
         while not stdin_ready():
             while evtloop.Pending():
+                t = clock()
-            time.sleep(0.01) # Change this to tune performance
+            if clock() - t > 0.1:
+                # no input is happening, we can sleep as much as we want
+                time.sleep(0.05)
+            else:
+                # input is happening, either wx (e.g. mouse) or keyboard, so
+                # sleep only very little
+                time.sleep(0.001)
         del ea
     return 0

Now if no input is happening, the "sleep(0.05)" version is running,
thus it has very low CPU usage. If however some input is happening
(either matplotlib, or ipython), then we just sleep(0.001), maybe we
don't have to sleep at all, I am not sure about this.

In any case, this should fix Gael's objection.


More information about the IPython-dev mailing list