##// END OF EJS Templates
Backport PR #2294: inputhook_qt4: Use QEventLoop instead of starting up the QCoreApplication...
Backport PR #2294: inputhook_qt4: Use QEventLoop instead of starting up the QCoreApplication I referenced this branch in #2080 and was letting it sit for a little while, but I have decided to make it a full pull request to get some additional visibility. Essentially our Qt event loop mechanism repeatedly starts and quits a `QCoreApplication` object. Unfortunately the `QCoreApplication::quit` slot has a lot of unintended side effects (like emitting an `aboutToQuit` signal which closes all open file dialogs). For our input hook, we _might_ be able to get by with just using a `QEventLoop` whose quit slot is much simpler and less destructive. For a little bit of background on why one might want to just use `QEventLoop::exec`, let's examine what `QCoreApplication::exec` does: ```c++ int QCoreApplication::exec() { if (!QCoreApplicationPrivate::checkInstance("exec")) return -1; // ... [some assertions] threadData->quitNow = false; QEventLoop eventLoop; self->d_func()->in_exec = true; self->d_func()->aboutToQuitEmitted = false; int returnCode = eventLoop.exec(); threadData->quitNow = false; if (self) { self->d_func()->in_exec = false; if (!self->d_func()->aboutToQuitEmitted) emit self->aboutToQuit(); self->d_func()->aboutToQuitEmitted = true; sendPostedEvents(0, QEvent::DeferredDelete); } return returnCode; } ``` As far as I can tell, it's a small wrapper around `QEventLoop::exec` which also: * Sets some variables regarding the current status * Emits an `aboutToQuit` signal right before the function returns (which is the root cause of @denisri's problem in #2080). Historically, our Qt event loop is a python implementation of the (win 32) input hook supplied with the PyQt4 source (see qtcore_input_hook` in `python-qt4/sip/QtCore/qcoreapplication.sip`), which more or less dates to a [mailing list post](http://www.riverbankcomputing.com/pipermail/pyqt/2007-July/016512.html) from July 2007.

File last commit:

r7079:beaf65e4
r9834:71196839
Show More
base_frontend_mixin.py
121 lines | 4.9 KiB | text/x-python | PythonLexer
/ IPython / frontend / qt / base_frontend_mixin.py
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 """ Defines a convenient mix-in class for implementing Qt frontends.
"""
class BaseFrontendMixin(object):
""" A mix-in class for implementing Qt frontends.
To handle messages of a particular type, frontends need only define an
appropriate handler method. For example, to handle 'stream' messaged, define
a '_handle_stream(msg)' method.
"""
#---------------------------------------------------------------------------
# 'BaseFrontendMixin' concrete interface
#---------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 def _get_kernel_manager(self):
""" Returns the current kernel manager.
"""
return self._kernel_manager
def _set_kernel_manager(self, kernel_manager):
""" Disconnect from the current kernel manager (if any) and set a new
kernel manager.
"""
# Disconnect the old kernel manager, if necessary.
old_manager = self._kernel_manager
if old_manager is not None:
MinRK
add kernel_started signal to QtKernelManager...
r7079 old_manager.started_kernel.disconnect(self._started_kernel)
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 old_manager.started_channels.disconnect(self._started_channels)
old_manager.stopped_channels.disconnect(self._stopped_channels)
# Disconnect the old kernel manager's channels.
old_manager.sub_channel.message_received.disconnect(self._dispatch)
MinRK
cleanup channel names to match function not socket...
r3974 old_manager.shell_channel.message_received.disconnect(self._dispatch)
old_manager.stdin_channel.message_received.disconnect(self._dispatch)
epatters
* Improved frontend-side kernel restart support....
r2913 old_manager.hb_channel.kernel_died.disconnect(
self._handle_kernel_died)
Bernardo B. Marques
remove all trailling spaces
r4872
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 # Handle the case where the old kernel manager is still listening.
if old_manager.channels_running:
self._stopped_channels()
# Set the new kernel manager.
self._kernel_manager = kernel_manager
if kernel_manager is None:
return
# Connect the new kernel manager.
MinRK
add kernel_started signal to QtKernelManager...
r7079 kernel_manager.started_kernel.connect(self._started_kernel)
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 kernel_manager.started_channels.connect(self._started_channels)
kernel_manager.stopped_channels.connect(self._stopped_channels)
# Connect the new kernel manager's channels.
kernel_manager.sub_channel.message_received.connect(self._dispatch)
MinRK
cleanup channel names to match function not socket...
r3974 kernel_manager.shell_channel.message_received.connect(self._dispatch)
kernel_manager.stdin_channel.message_received.connect(self._dispatch)
Brian Granger
Added heartbeat support.
r2910 kernel_manager.hb_channel.kernel_died.connect(self._handle_kernel_died)
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 # Handle the case where the kernel manager started channels before
# we connected.
if kernel_manager.channels_running:
self._started_channels()
kernel_manager = property(_get_kernel_manager, _set_kernel_manager)
#---------------------------------------------------------------------------
# 'BaseFrontendMixin' abstract interface
#---------------------------------------------------------------------------
epatters
* Improved frontend-side kernel restart support....
r2913
def _handle_kernel_died(self, since_last_heartbeat):
""" This is called when the ``kernel_died`` signal is emitted.
This method is called when the kernel heartbeat has not been
active for a certain amount of time. The typical action will be to
give the user the option of restarting the kernel.
Parameters
----------
since_last_heartbeat : float
The time since the heartbeat was last received.
"""
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
add kernel_started signal to QtKernelManager...
r7079 def _started_kernel(self):
"""Called when the KernelManager starts (or restarts) the kernel subprocess.
Channels may or may not be running at this point.
"""
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 def _started_channels(self):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Called when the KernelManager channels have started listening or
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 when the frontend is assigned an already listening KernelManager.
"""
def _stopped_channels(self):
""" Called when the KernelManager channels have stopped listening or
when a listening KernelManager is removed from the frontend.
"""
#---------------------------------------------------------------------------
epatters
* The Qt console frontend now ignores cross chatter from other frontends....
r2824 # 'BaseFrontendMixin' protected interface
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 #---------------------------------------------------------------------------
def _dispatch(self, msg):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Calls the frontend handler associated with the message type of the
epatters
* The Qt console frontend now ignores cross chatter from other frontends....
r2824 given message.
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 """
Brian E. Granger
Fixing code to assume msg_type and msg_id are top-level....
r4230 msg_type = msg['header']['msg_type']
epatters
* Moved KernelManager attribute management code in FrontendWidget into a mixin class usable in any Qt frontend. Registering handlers for message types is now trivial....
r2770 handler = getattr(self, '_handle_' + msg_type, None)
if handler:
handler(msg)
epatters
* The Qt console frontend now ignores cross chatter from other frontends....
r2824
def _is_from_this_session(self, msg):
""" Returns whether a reply from the kernel originated from a request
from this frontend.
"""
session = self._kernel_manager.session.session
MinRK
associate messages with no parent to all qt frontends...
r5362 parent = msg['parent_header']
if not parent:
# if the message has no parent, assume it is meant for all frontends
return True
else:
return parent.get('session') == session