7777
7878# End of the great "support IPython 2, 3, 4" strat
7979
80+
8081def _debug_write (out ):
8182 if DEBUG :
8283 sys .__stdout__ .write (out )
@@ -140,7 +141,8 @@ def __init__(self, kernelUUID, socketConnection, send_lock, shell_channel, iopub
140141 self .shell_channel = shell_channel
141142 self .iopub_channel = iopub_channel
142143 self .send_lock = send_lock
143- thread .start_new_thread (self .start_processing , ())
144+ thread .start_new_thread (self .start_processing , ('shell' , 1 ))
145+ thread .start_new_thread (self .start_processing , ('io' , 1 ))
144146
145147 def stop (self ):
146148 self .is_stop_requested = True
@@ -156,7 +158,7 @@ def _populateErrorContents(self, sourceContents, targetContents):
156158 except AttributeError :
157159 pass
158160
159- def start_processing (self ):
161+ def start_processing (self , channel , * args ):
160162 """loop to read the io ports/messages"""
161163
162164 _debug_write ('Started processing thread' )
@@ -165,30 +167,32 @@ def start_processing(self):
165167 if self .check_for_exit_socket_loop ():
166168 break
167169
168- try :
169- # We can ignore msgtype=execute_request
170-
171- exe_result = self .shell_channel .get_shell_msg (timeout = 1 )
172- # message can be JSON, but not always
173- # (http://jupyter-client.readthedocs.io/en/latest/messaging.html)
174- # assume for now that dates are the only crappy (non JSONable stuff sent)
175- json_to_send = json .dumps (exe_result , default = str )
176- with self .send_lock :
177- _debug_write ('shell_result' )
178- write_bytes (self .conn , iPythonSocketServer ._SHEL )
179- write_string (self .conn , json_to_send )
180- except Empty :
181- pass
182-
183- try :
184- msg = self .iopub_channel .get_iopub_msg (timeout = 10 )
185- json_to_send = json .dumps (msg , default = str )
186- with self .send_lock :
187- _debug_write ('iopub_msg' )
188- write_bytes (self .conn , iPythonSocketServer ._IOPB )
189- write_string (self .conn , json_to_send )
190- except Empty :
191- pass
170+ # message can be JSON, but not always
171+ # (http://jupyter-client.readthedocs.io/en/latest/messaging.html)
172+ # assume for now that dates are the only crappy (non JSONable
173+ # stuff sent)
174+ if channel == 'shell' :
175+ try :
176+ exe_result = self .shell_channel .get_shell_msg (
177+ timeout = 30 )
178+ json_to_send = json .dumps (exe_result , default = str )
179+ with self .send_lock :
180+ _debug_write ('shell_result' )
181+ write_bytes (self .conn , iPythonSocketServer ._SHEL )
182+ write_string (self .conn , json_to_send )
183+ except Empty :
184+ pass
185+
186+ if channel == 'io' :
187+ try :
188+ msg = self .iopub_channel .get_iopub_msg (timeout = 30 )
189+ json_to_send = json .dumps (msg , default = str )
190+ with self .send_lock :
191+ _debug_write ('iopub_msg' )
192+ write_bytes (self .conn , iPythonSocketServer ._IOPB )
193+ write_string (self .conn , json_to_send )
194+ except Empty :
195+ pass
192196
193197 except IPythonExitException :
194198 _debug_write ('IPythonExitException' )
@@ -294,7 +298,8 @@ def start_processing(self):
294298 except :
295299 commandName = utf_8 .decode (inp )[0 ]
296300 try :
297- commandName = ascii .Codec .encode (commandName )[0 ]
301+ commandName = ascii .Codec .encode (commandName )[
302+ 0 ]
298303 except UnicodeEncodeError :
299304 pass
300305 self .replyWithError (commandName , id )
@@ -406,13 +411,15 @@ def _postStartKernel(self, kernelUUID):
406411 kernel_client .wait_for_ready ()
407412 iopub = kernel_client
408413 shell = kernel_client
414+ # todo: get_stdin_msg
409415 except AttributeError :
410416 # Ipython 2.x
411417 # Based on https://github.com/paulgb/runipy/pull/49/files
412418 iopub = kernel_client .iopub_channel
413419 shell = kernel_client .shell_channel
414420 shell .get_shell_msg = shell .get_msg
415421 iopub .get_iopub_msg = iopub .get_msg
422+ # todo: get_stdin_msg
416423
417424 self .shell_channel = shell
418425 self .kernelMonitor = iPythonKernelResponseMonitor (
0 commit comments