2121
2222from tornado import ioloop
2323from tornado import gen
24- from tornado .queues import PriorityQueue , QueueEmpty
24+ from tornado .queues import Queue , QueueEmpty
2525import zmq
2626from zmq .eventloop .zmqstream import ZMQStream
2727
3838
3939from ._version import kernel_protocol_version
4040
41- CONTROL_PRIORITY = 1
42- SHELL_PRIORITY = 10
43-
4441
4542class Kernel (SingletonConfigurable ):
4643
@@ -60,7 +57,7 @@ def _update_eventloop(self, change):
6057
6158 session = Instance (Session , allow_none = True )
6259 profile_dir = Instance ('IPython.core.profiledir.ProfileDir' , allow_none = True )
63- shell_streams = List ( )
60+ shell_stream = Instance ( ZMQStream , allow_none = True )
6461 control_stream = Instance (ZMQStream , allow_none = True )
6562 iopub_socket = Any ()
6663 iopub_thread = Any ()
@@ -215,7 +212,7 @@ def should_handle(self, stream, msg, idents):
215212 return True
216213
217214 @gen .coroutine
218- def dispatch_shell (self , stream , msg ):
215+ def dispatch_shell (self , msg ):
219216 """dispatch shell requests"""
220217 idents , msg = self .session .feed_identities (msg , copy = False )
221218 try :
@@ -232,11 +229,11 @@ def dispatch_shell(self, stream, msg):
232229
233230 # Only abort execute requests
234231 if self ._aborting and msg_type == 'execute_request' :
235- self ._send_abort_reply (stream , msg , idents )
232+ self ._send_abort_reply (self . shell_stream , msg , idents )
236233 self ._publish_status ('idle' )
237234 # flush to ensure reply is sent before
238235 # handling the next request
239- stream .flush (zmq .POLLOUT )
236+ self . shell_stream .flush (zmq .POLLOUT )
240237 return
241238
242239 # Print some info about this message and leave a '--->' marker, so it's
@@ -245,7 +242,7 @@ def dispatch_shell(self, stream, msg):
245242 self .log .debug ('\n *** MESSAGE TYPE:%s***' , msg_type )
246243 self .log .debug (' Content: %s\n --->\n ' , msg ['content' ])
247244
248- if not self .should_handle (stream , msg , idents ):
245+ if not self .should_handle (self . shell_stream , msg , idents ):
249246 return
250247
251248 handler = self .shell_handlers .get (msg_type , None )
@@ -258,7 +255,7 @@ def dispatch_shell(self, stream, msg):
258255 except Exception :
259256 self .log .debug ("Unable to signal in pre_handler_hook:" , exc_info = True )
260257 try :
261- yield gen .maybe_future (handler (stream , idents , msg ))
258+ yield gen .maybe_future (handler (self . shell_stream , idents , msg ))
262259 except Exception :
263260 self .log .error ("Exception in message handler:" , exc_info = True )
264261 finally :
@@ -272,7 +269,7 @@ def dispatch_shell(self, stream, msg):
272269 self ._publish_status ('idle' )
273270 # flush to ensure reply is sent before
274271 # handling the next request
275- stream .flush (zmq .POLLOUT )
272+ self . shell_stream .flush (zmq .POLLOUT )
276273
277274 def pre_handler_hook (self ):
278275 """Hook to execute before calling message handler"""
@@ -332,27 +329,22 @@ def do_one_iteration(self):
332329 .. versionchanged:: 5
333330 This is now a coroutine
334331 """
335- # flush messages off of shell streams into the message queue
336- for stream in self .shell_streams :
337- stream .flush ()
338- # process all messages higher priority than shell (control),
339- # and at most one shell message per iteration
340- priority = 0
341- while priority is not None and priority < SHELL_PRIORITY :
342- priority = yield self .process_one (wait = False )
332+ # flush messages off of shell stream into the message queue
333+ self .shell_stream .flush ()
334+ # process at most one shell message per iteration
335+ yield self .process_one (wait = False )
343336
344337 @gen .coroutine
345338 def process_one (self , wait = True ):
346339 """Process one request
347340
348- Returns priority of the message handled.
349341 Returns None if no message was handled.
350342 """
351343 if wait :
352- priority , t , dispatch , args = yield self .msg_queue .get ()
344+ t , dispatch , args = yield self .msg_queue .get ()
353345 else :
354346 try :
355- priority , t , dispatch , args = self .msg_queue .get_nowait ()
347+ t , dispatch , args = self .msg_queue .get_nowait ()
356348 except QueueEmpty :
357349 return None
358350 yield gen .maybe_future (dispatch (* args ))
@@ -377,21 +369,18 @@ def dispatch_queue(self):
377369
378370 _message_counter = Any (
379371 help = """Monotonic counter of messages
380-
381- Ensures messages of the same priority are handled in arrival order.
382372 """ ,
383373 )
384374 @default ('_message_counter' )
385375 def _message_counter_default (self ):
386376 return itertools .count ()
387377
388- def schedule_dispatch (self , priority , dispatch , * args ):
378+ def schedule_dispatch (self , dispatch , * args ):
389379 """schedule a message for dispatch"""
390380 idx = next (self ._message_counter )
391381
392382 self .msg_queue .put_nowait (
393383 (
394- priority ,
395384 idx ,
396385 dispatch ,
397386 args ,
@@ -403,32 +392,24 @@ def schedule_dispatch(self, priority, dispatch, *args):
403392 def start (self ):
404393 """register dispatchers for streams"""
405394 self .io_loop = ioloop .IOLoop .current ()
406- self .msg_queue = PriorityQueue ()
395+ self .msg_queue = Queue ()
407396 self .io_loop .add_callback (self .dispatch_queue )
408397
398+ self .control_stream .on_recv (
399+ partial (
400+ self .schedule_dispatch ,
401+ self .dispatch_control ,
402+ ),
403+ copy = False ,
404+ )
409405
410- if self .control_stream :
411- self .control_stream .on_recv (
412- partial (
413- self .schedule_dispatch ,
414- CONTROL_PRIORITY ,
415- self .dispatch_control ,
416- ),
417- copy = False ,
418- )
419-
420- for s in self .shell_streams :
421- if s is self .control_stream :
422- continue
423- s .on_recv (
424- partial (
425- self .schedule_dispatch ,
426- SHELL_PRIORITY ,
427- self .dispatch_shell ,
428- s ,
429- ),
430- copy = False ,
431- )
406+ self .shell_stream .on_recv (
407+ partial (
408+ self .schedule_dispatch ,
409+ self .dispatch_shell ,
410+ ),
411+ copy = False ,
412+ )
432413
433414 # publish idle status
434415 self ._publish_status ('starting' )
@@ -784,8 +765,7 @@ def _topic(self, topic):
784765
785766 @gen .coroutine
786767 def _abort_queues (self ):
787- for stream in self .shell_streams :
788- stream .flush ()
768+ self .shell_stream .flush ()
789769 self ._aborting = True
790770
791771 def stop_aborting (f ):
@@ -909,4 +889,4 @@ def _at_shutdown(self):
909889 if self ._shutdown_message is not None :
910890 self .session .send (self .iopub_socket , self ._shutdown_message , ident = self ._topic ('shutdown' ))
911891 self .log .debug ("%s" , self ._shutdown_message )
912- [ s . flush (zmq .POLLOUT ) for s in self . shell_streams ]
892+ self . shell_stream . flush (zmq .POLLOUT )
0 commit comments