Skip to content

Commit

Permalink
Fix in-process kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Apr 7, 2022
1 parent f81f481 commit 76ee4ac
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 20 deletions.
1 change: 0 additions & 1 deletion examples/embedding/inprocess_qtconsole.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys

from IPython.lib import guisupport
from qtconsole.inprocess import QtInProcessKernelManager
Expand Down
1 change: 0 additions & 1 deletion examples/embedding/inprocess_terminal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys

from jupyter_console.ptshell import ZMQTerminalInteractiveShell

Expand Down
1 change: 0 additions & 1 deletion ipykernel/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from threading import Thread



class ControlThread(Thread):
def __init__(self, **kwargs):
Thread.__init__(self, name="Control", **kwargs)
Expand Down
2 changes: 0 additions & 2 deletions ipykernel/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import zmq
from IPython.core.getipython import get_ipython
from IPython.core.inputtransformer2 import leading_empty_lines
from tornado.locks import Event
from tornado.queues import Queue
from zmq.utils import jsonapi

try:
Expand Down
9 changes: 6 additions & 3 deletions ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import zmq
from jupyter_client.session import extract_header
from zmq.eventloop.zmqstream import ZMQStream

from .zmqstream import ZMQStream

# -----------------------------------------------------------------------------
# Globals
Expand Down Expand Up @@ -184,7 +185,7 @@ def stop(self):
if not self.thread.is_alive():
return
self.io_loop.call_soon_threadsafe(self.io_loop.stop)
self.thread.join()
# self.thread.join()
# close *all* event pipes, created in any thread
# event pipes can only be used from other threads while self.thread.is_alive()
# so after thread.join, this should be safe
Expand Down Expand Up @@ -458,7 +459,9 @@ def _schedule_flush(self):
def _schedule_in_thread():
# FIXME: original code was:
# self._io_loop.call_later(self.flush_interval, self._flush)
self._io_loop.call_soon_threadsafe(self._io_loop.call_later, self.flush_interval, self._flush)
self._io_loop.call_soon_threadsafe(
self._io_loop.call_later, self.flush_interval, self._flush
)

self.pub_thread.schedule(_schedule_in_thread)

Expand Down
6 changes: 2 additions & 4 deletions ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from IPython.core import release
from IPython.utils.tokenutil import line_at_cursor, token_at_cursor
from traitlets import Any, Bool, Instance, List, Type, observe, observe_compat
from zmq.eventloop.zmqstream import ZMQStream

from .comm import CommManager
from .compiler import XCachingCompiler
Expand All @@ -20,6 +19,7 @@
from .kernelbase import Kernel as KernelBase
from .kernelbase import _accepts_cell_id
from .zmqshell import ZMQInteractiveShell
from .zmqstream import ZMQStream

try:
from IPython.core.interactiveshell import _asyncio_runner
Expand Down Expand Up @@ -182,9 +182,7 @@ def start(self):
self.debugpy_stream.on_recv(self.dispatch_debugpy, copy=False)
super().start()
if self.debugpy_stream:
asyncio.run_coroutine_threadsafe(
self.poll_stopped_queue(), self.control_thread.io_loop
)
asyncio.run_coroutine_threadsafe(self.poll_stopped_queue(), self.control_thread.io_loop)

def set_parent(self, ident, parent, channel="shell"):
"""Overridden from parent to tell the display hook and output streams
Expand Down
3 changes: 0 additions & 3 deletions ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import zmq
import zmq.asyncio

from IPython.core.application import (
BaseIPythonApplication,
base_aliases,
Expand All @@ -30,7 +29,6 @@
from jupyter_client.connect import ConnectionFileMixin
from jupyter_client.session import Session, session_aliases, session_flags
from jupyter_core.paths import jupyter_runtime_dir
from tornado import ioloop
from traitlets import (
Any,
Bool,
Expand All @@ -44,7 +42,6 @@
)
from traitlets.utils import filefind
from traitlets.utils.importstring import import_item
from zmq.eventloop.zmqstream import ZMQStream

from .control import ControlThread
from .heartbeat import Heartbeat
Expand Down
2 changes: 1 addition & 1 deletion ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _update_eventloop(self, change):
"""schedule call to eventloop from IOLoop"""
loop = asyncio.get_event_loop()
if change.new is not None:
loop.call_soon(self.enter_eventloop())
loop.call_soon(self.enter_eventloop)

session = Instance(Session, allow_none=True)
profile_dir = Instance("IPython.core.profiledir.ProfileDir", allow_none=True)
Expand Down
2 changes: 0 additions & 2 deletions ipykernel/tests/test_eventloop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test eventloop integration"""

import pytest

from .utils import execute, flush_channels, start_new_kernel

KC = KM = None
Expand Down
4 changes: 3 additions & 1 deletion ipykernel/trio_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def initialize(self, kernel, io_loop):
kernel.shell.magics_manager.magics["line"]["autoawait"] = lambda _: warnings.warn(
"Autoawait isn't allowed in Trio background loop mode."
)
bg_thread = threading.Thread(target=io_loop.run_forever, daemon=True, name="AsyncioBackground")
bg_thread = threading.Thread(
target=io_loop.run_forever, daemon=True, name="AsyncioBackground"
)
bg_thread.start()

def interrupt(self, signum, frame):
Expand Down
6 changes: 5 additions & 1 deletion ipykernel/zmqstream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from inspect import isawaitable


class ZMQStream:
Expand All @@ -21,7 +22,10 @@ def on_recv(self, callback, copy=True):

async def recv_task(self, callback, copy):
while True:
msg_list = await self.socket.recv_multipart(copy=copy)
msg_list = self.socket.recv_multipart(copy=copy)
if isawaitable(msg_list):
# in-process kernels are not async
msg_list = await msg_list
callback(msg_list)

def send_multipart(self, msg_list, flags=0, copy=True, track=False, **kwargs):
Expand Down

0 comments on commit 76ee4ac

Please sign in to comment.