Skip to content

Commit b763d54

Browse files
sonthonaxrkSylvainCorlay
authored andcommitted
Issue #12786: Create hook for dispatching messages out of order
1 parent d6c78de commit b763d54

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

ipykernel/inprocess/client.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ def _dispatch_to_kernel(self, msg):
171171
raise RuntimeError('Cannot send request. No kernel exists.')
172172

173173
stream = kernel.shell_stream
174-
self.session.send(stream, msg)
175-
msg_parts = stream.recv_multipart()
176-
kernel.dispatch_shell(msg_parts)
177-
174+
kernel.dispatch_shell(msg)
178175
idents, reply_msg = self.session.recv(stream, copy=False)
179176
self.shell_channel.call_handlers_later(reply_msg)
180177

ipykernel/kernelbase.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,10 @@ def should_handle(self, stream, msg, idents):
240240
return True
241241

242242
@gen.coroutine
243-
def dispatch_shell(self, msg):
243+
def dispatch_shell(self, msg, idents=None):
244244
"""dispatch shell requests"""
245-
idents, msg = self.session.feed_identities(msg, copy=False)
246-
try:
247-
msg = self.session.deserialize(msg, content=True, copy=False)
248-
except:
249-
self.log.error("Invalid Message", exc_info=True)
250-
return
245+
if idents is None:
246+
idents = []
251247

252248
# Set the parent message for side effects.
253249
self.set_parent(idents, msg, channel='shell')
@@ -403,15 +399,38 @@ def dispatch_queue(self):
403399
def _message_counter_default(self):
404400
return itertools.count()
405401

406-
def schedule_dispatch(self, dispatch, *args):
402+
def should_dispatch_immediately(self, msg):
403+
"""
404+
This provides a hook for dispatching incoming messages
405+
from the frontend immediately, and out of order.
406+
407+
It could be used to allow asynchronous messages from
408+
GUIs to be processed.
409+
"""
410+
return False
411+
412+
def schedule_dispatch(self, msg, dispatch):
407413
"""schedule a message for dispatch"""
414+
415+
idents, msg = self.session.feed_identities(msg, copy=False)
416+
try:
417+
msg = self.session.deserialize(msg, content=True, copy=False)
418+
except:
419+
self.log.error("Invalid shell message", exc_info=True)
420+
return
421+
422+
new_args = (msg, idents)
423+
424+
if self.should_dispatch_immediately(msg):
425+
return self.io_loop.add_callback(dispatch, *new_args)
426+
408427
idx = next(self._message_counter)
409428

410429
self.msg_queue.put_nowait(
411430
(
412431
idx,
413432
dispatch,
414-
args,
433+
new_args,
415434
)
416435
)
417436
# ensure the eventloop wakes up
@@ -428,7 +447,7 @@ def start(self):
428447
self.shell_stream.on_recv(
429448
partial(
430449
self.schedule_dispatch,
431-
self.dispatch_shell,
450+
dispatch=self.dispatch_shell,
432451
),
433452
copy=False,
434453
)

0 commit comments

Comments
 (0)