Skip to content

Commit ff34039

Browse files
committed
Adapt to jupyter_client >= 7
See: jupyter/jupyter_console#244
1 parent 73f7a2c commit ff34039

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

rnbgrader/kernels.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
from PIL import Image
1919
from jupyter_client.manager import start_new_kernel
2020

21+
# https://github.com/jupyter/jupyter_console/pull/244
22+
import jupyter_client
23+
24+
# jupyter_client 7.0+ has async channel methods that we expect to be sync here
25+
JUPYTER_CLIENT_7 = jupyter_client.version_info >= (7,)
26+
if JUPYTER_CLIENT_7:
27+
from jupyter_client.utils import run_sync
28+
else:
29+
run_sync = lambda x: x
30+
31+
2132
DEFAULT_TIMEOUT = 30
2233

2334

@@ -66,10 +77,13 @@ def __del__(self):
6677

6778
def flush_channels(self):
6879
""" Flush all kernel channels """
80+
kwargs = {'timeout': 0.1}
81+
if not JUPYTER_CLIENT_7:
82+
kwargs['block'] = True
6983
for channel in (self.client.shell_channel, self.client.iopub_channel):
7084
while True:
7185
try:
72-
channel.get_msg(block=True, timeout=0.1)
86+
run_sync(channel.get_msg)(**kwargs)
7387
except Empty:
7488
break
7589

@@ -110,12 +124,12 @@ def raw_run(self, code, timeout=None,
110124

111125
reply = kc.get_shell_msg(timeout=timeout)
112126

113-
busy_msg = kc.iopub_channel.get_msg(timeout=1)
127+
busy_msg = run_sync(kc.iopub_channel.get_msg)(timeout=1)
114128
assert busy_msg['content']['execution_state'] == 'busy'
115129

116130
output_msgs = []
117131
while True:
118-
msg = kc.iopub_channel.get_msg(timeout=0.1)
132+
msg = run_sync(kc.iopub_channel.get_msg)(timeout=0.1)
119133
if msg['msg_type'] == 'status':
120134
assert msg['content']['execution_state'] == 'idle'
121135
break

0 commit comments

Comments
 (0)