Permalink
Browse files

inputhookqt4: improve responsiveness when pasting

The Qt4 inputhook now execs until the stdin is readable, instead of
waiting for a timeout to happen. This approach was supposed to play
better with readline which repeatedly calls the inputhook every 100ms.
But actually it does not. Same timings as before, roughly twice the
time it takes when there's no %gui. The timing for the original native
PyQt4 hook was somewhere in the middle (35ms - 58ms - 72ms).
  • Loading branch information...
1 parent 52fc956 commit 7f643b3f7f5dfd6a0e19dd03b55e7ae4693e6d37 @cboos committed Dec 5, 2011
Showing with 25 additions and 6 deletions.
  1. +25 −6 IPython/lib/inputhookqt4.py
@@ -16,14 +16,38 @@
# Imports
#-----------------------------------------------------------------------------
+import os
+import sys
+
from IPython.core.interactiveshell import InteractiveShell
from IPython.external.qt_for_kernel import QtCore, QtGui
from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready
+try:
+ import readline
+ has_readline = True
+except ImportError:
+ has_readline = False
+
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
+if os.name != 'nt' and has_readline:
+ def qt4_exec_until_stdin_ready(app):
+ notifier = QtCore.QSocketNotifier(sys.stdin.fileno(), QtCore.QSocketNotifier.Read)
+ notifier.activated.connect(app.quit)
+ app.exec_()
+else:
+ def qt4_exec_until_stdin_ready(app):
+ timer = QtCore.QTimer()
+ timer.timeout.connect(app.quit)
+ while not stdin_ready():
+ timer.start(50)
+ app.exec_()
+ timer.stop()
+
+
def create_inputhook_qt4(mgr, app=None):
"""Create an input hook for running the Qt4 application event loop.
@@ -85,12 +109,7 @@ def inputhook_qt4():
return 0
app.processEvents(QtCore.QEventLoop.AllEvents, 300)
if not stdin_ready():
- timer = QtCore.QTimer()
- timer.timeout.connect(app.quit)
- while not stdin_ready():
- timer.start(50)
- app.exec_()
- timer.stop()
+ qt4_exec_until_stdin_ready(app)
ignore_CTRL_C()
except KeyboardInterrupt:
ignore_CTRL_C()

0 comments on commit 7f643b3

Please sign in to comment.