Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions IPython/kernel/inprocess/asyncipkernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""An asynchronous in-process kernel"""
import threading
import time

from .ipkernel import InProcessKernel

def _execute_request(kernel, stream, ident, parent):
super(InProcessKernel, kernel).execute_request(stream, ident, parent)
return True


class AsyncInProcessKernel(InProcessKernel):
def execute_request(self, stream, ident, parent):
# Temporary IO redirection.
with self._redirected_io():
self.exec_in_thread(stream, ident, parent)

# Don not forget to add:
# "from gevent import monkey; monkey.patch_all(thread=False)"
# as one of the first import statements
#self.exec_in_coroutine(stream, ident, parent)

def exec_in_thread(self, stream, ident, parent):
task = threading.Thread(target=_execute_request,
args=(self, stream, ident, parent))
task.start()

while task.is_alive():
time.sleep(0.01)

# Assume that in-process kernels only have one frontend
self.frontends[0].iopub_channel.process_events()

def exec_in_coroutine(self, stream, ident, parent):
import gevent
task = gevent.spawn(_execute_request, self, stream, ident, parent)

while not task.value:
gevent.wait(timeout=0.01)
self.frontends[0].iopub_channel.process_events()
6 changes: 6 additions & 0 deletions IPython/qt/asyncinprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .inprocess import QtInProcessKernelManager

class QtAsyncInProcessKernelManager(QtInProcessKernelManager):
def start_kernel(self, **kwds):
from IPython.kernel.inprocess.asyncipkernel import AsyncInProcessKernel
self.kernel = AsyncInProcessKernel()
32 changes: 28 additions & 4 deletions IPython/qt/kernel_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from IPython.external.qt import QtCore
import sys

from IPython.external.qt import QtCore
from IPython.utils.traitlets import HasTraits, Type
from .util import MetaQObjectHasTraits, SuperQObject

Expand All @@ -17,6 +18,10 @@ class ChannelQObject(SuperQObject):
# Emitted when the channel is stopped.
stopped = QtCore.Signal()

#---------------------------------------------------------------------------
# Channel interface
#---------------------------------------------------------------------------

def start(self):
""" Reimplemented to emit signal.
"""
Expand All @@ -42,6 +47,8 @@ def call_handlers_later(self, *args, **kwds):
def process_events(self):
""" Process any pending GUI events.
"""
sys.stdout.flush()
sys.stderr.flush()
QtCore.QCoreApplication.instance().processEvents()


Expand All @@ -57,6 +64,10 @@ class QtShellChannelMixin(ChannelQObject):
history_reply = QtCore.Signal(object)
kernel_info_reply = QtCore.Signal(object)

#---------------------------------------------------------------------------
# 'ShellChannel' interface
#---------------------------------------------------------------------------

def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
Expand All @@ -65,9 +76,7 @@ def call_handlers(self, msg):

# Emit signals for specialized message types.
msg_type = msg['header']['msg_type']
if msg_type == 'kernel_info_reply':
self._handle_kernel_info_reply(msg)


signal = getattr(self, msg_type, None)
if signal:
signal.emit(msg)
Expand Down Expand Up @@ -100,6 +109,10 @@ class QtIOPubChannelMixin(ChannelQObject):
# Emitted when a shutdown is noticed.
shutdown_reply_received = QtCore.Signal(object)

#---------------------------------------------------------------------------
# 'IOPubChannel' interface
#---------------------------------------------------------------------------

def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
Expand All @@ -110,6 +123,8 @@ def call_handlers(self, msg):
signal = getattr(self, msg_type + '_received', None)
if signal:
signal.emit(msg)
elif msg_type in ('stdout', 'stderr'):
self.stream_received.emit(msg)

def flush(self):
""" Reimplemented to ensure that signals are dispatched immediately.
Expand All @@ -126,6 +141,10 @@ class QtStdInChannelMixin(ChannelQObject):
# Emitted when an input request is received.
input_requested = QtCore.Signal(object)

#---------------------------------------------------------------------------
# 'StdInChannel' interface
#---------------------------------------------------------------------------

def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
Expand All @@ -143,9 +162,14 @@ class QtHBChannelMixin(ChannelQObject):
# Emitted when the kernel has died.
kernel_died = QtCore.Signal(object)

#---------------------------------------------------------------------------
# 'HBChannel' interface
#---------------------------------------------------------------------------

def call_handlers(self, since_last_heartbeat):
""" Reimplemented to emit signals instead of making callbacks.
"""
# Emit the generic signal.
self.kernel_died.emit(since_last_heartbeat)


Expand Down
46 changes: 46 additions & 0 deletions examples/Embedding/async_inprocess_qtconsole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import print_function
import os

from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.asyncinprocess import QtAsyncInProcessKernelManager
from IPython.lib import guisupport


def print_process_id():
print('Process ID is:', os.getpid())


def main():
# Print the ID of the main process
print_process_id()

app = guisupport.get_app_qt4()

# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel_manager = QtAsyncInProcessKernelManager()
kernel_manager.start_kernel()
kernel = kernel_manager.kernel
kernel.gui = 'qt4'
kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})

kernel_client = kernel_manager.client()
kernel_client.start_channels()

def stop():
kernel_client.stop_channels()
kernel_manager.shutdown_kernel()
app.exit()

control = RichIPythonWidget()
control.kernel_manager = kernel_manager
control.kernel_client = kernel_client
control.exit_requested.connect(stop)
control.show()

guisupport.start_event_loop_qt4(app)


if __name__ == '__main__':
main()