@@ -331,10 +331,7 @@ async def process_control(self, msg):
331
331
332
332
sys .stdout .flush ()
333
333
sys .stderr .flush ()
334
- self ._publish_status ("idle" , "control" )
335
- # flush to ensure reply is sent
336
- if self .control_stream :
337
- self .control_stream .flush (zmq .POLLOUT )
334
+ self ._publish_status_and_flush ("idle" , "control" , self .control_stream )
338
335
339
336
def should_handle (self , stream , msg , idents ):
340
337
"""Check whether a shell-channel message should be handled
@@ -370,11 +367,7 @@ async def dispatch_shell(self, msg):
370
367
# Only abort execute requests
371
368
if self ._aborting and msg_type == "execute_request" :
372
369
self ._send_abort_reply (self .shell_stream , msg , idents )
373
- self ._publish_status ("idle" , "shell" )
374
- # flush to ensure reply is sent before
375
- # handling the next request
376
- if self .shell_stream :
377
- self .shell_stream .flush (zmq .POLLOUT )
370
+ self ._publish_status_and_flush ("idle" , "shell" , self .shell_stream )
378
371
return
379
372
380
373
# Print some info about this message and leave a '--->' marker, so it's
@@ -384,6 +377,7 @@ async def dispatch_shell(self, msg):
384
377
self .log .debug (" Content: %s\n --->\n " , msg ["content" ])
385
378
386
379
if not self .should_handle (self .shell_stream , msg , idents ):
380
+ self ._publish_status_and_flush ("idle" , "shell" , self .shell_stream )
387
381
return
388
382
389
383
handler = self .shell_handlers .get (msg_type , None )
@@ -412,11 +406,7 @@ async def dispatch_shell(self, msg):
412
406
413
407
sys .stdout .flush ()
414
408
sys .stderr .flush ()
415
- self ._publish_status ("idle" , "shell" )
416
- # flush to ensure reply is sent before
417
- # handling the next request
418
- if self .shell_stream :
419
- self .shell_stream .flush (zmq .POLLOUT )
409
+ self ._publish_status_and_flush ("idle" , "shell" , self .shell_stream )
420
410
421
411
def pre_handler_hook (self ):
422
412
"""Hook to execute before calling message handler"""
@@ -603,6 +593,12 @@ def _publish_status(self, status, channel, parent=None):
603
593
ident = self ._topic ("status" ),
604
594
)
605
595
596
+ def _publish_status_and_flush (self , status , channel , stream , parent = None ):
597
+ """send status on IOPub and flush specified stream to ensure reply is sent before handling the next reply"""
598
+ self ._publish_status (status , channel , parent )
599
+ if stream :
600
+ stream .flush (zmq .POLLOUT )
601
+
606
602
def _publish_debug_event (self , event ):
607
603
if not self .session :
608
604
return
0 commit comments