@@ -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