@@ -292,6 +292,13 @@ def __init__(self, **kwargs):
292292 self ._do_exec_accepted_params = _accepts_parameters (
293293 self .do_execute , ["cell_meta" , "cell_id" ]
294294 )
295+
296+ def _publish_status_and_flush (self , status , channel , stream ):
297+ self ._publish_status (status , channel )
298+ # flush to ensure reply is sent before
299+ # handling the next request
300+ if stream :
301+ stream .flush (zmq .POLLOUT )
295302
296303 async def dispatch_control (self , msg ):
297304 # Ensure only one control message is processed at a time
@@ -331,10 +338,7 @@ async def process_control(self, msg):
331338
332339 sys .stdout .flush ()
333340 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 )
341+ self ._publish_status_and_flush ("idle" , "control" , self .control_stream )
338342
339343 def should_handle (self , stream , msg , idents ):
340344 """Check whether a shell-channel message should be handled
@@ -370,11 +374,7 @@ async def dispatch_shell(self, msg):
370374 # Only abort execute requests
371375 if self ._aborting and msg_type == "execute_request" :
372376 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 )
377+ self ._publish_status_and_flush ("idle" , "shell" , self .shell_stream )
378378 return
379379
380380 # Print some info about this message and leave a '--->' marker, so it's
@@ -384,7 +384,7 @@ async def dispatch_shell(self, msg):
384384 self .log .debug (" Content: %s\n --->\n " , msg ["content" ])
385385
386386 if not self .should_handle (self .shell_stream , msg , idents ):
387- self ._publish_status ("idle" , "shell" )
387+ self ._publish_status_and_flush ("idle" , "shell" , self . shell_stream )
388388 return
389389
390390 handler = self .shell_handlers .get (msg_type , None )
@@ -413,11 +413,7 @@ async def dispatch_shell(self, msg):
413413
414414 sys .stdout .flush ()
415415 sys .stderr .flush ()
416- self ._publish_status ("idle" , "shell" )
417- # flush to ensure reply is sent before
418- # handling the next request
419- if self .shell_stream :
420- self .shell_stream .flush (zmq .POLLOUT )
416+ self ._publish_status_and_flush ("idle" , "shell" , self .shell_stream )
421417
422418 def pre_handler_hook (self ):
423419 """Hook to execute before calling message handler"""
0 commit comments