Skip to content

Commit e6d3c08

Browse files
committed
better handling for 'dead' messages from kernel
1 parent 2eb4ac2 commit e6d3c08

4 files changed

Lines changed: 41 additions & 23 deletions

File tree

TODO.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
- Completion on file names and directories should work better
2+
- Better control for scrolling: if I have large ouput in the last cell that suddenly gets replaced, then my scroll position is screwed up
3+
- Maybe shuold keep scroll position fixed while changing output!
4+
- Unclosed parenthesis still break highlighting (""" for python, and ' + " for R cells)
5+
- Initial scroll position is bad for empty notebooks
16
- Support set_next_input
2-
- Correctly handle 'dead' message from the kernel
37
- Add an option of saving a backup copy of notebook json file (use git to have all copies?):
48
- For now it is possible to use inb_open_as_ipynb command
5-
- Completion on file names and directories should work better
69
- Support image preview in some external program

ipy_connection.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ def __init__(self, notebook_id, baseurl):
257257
self.message_callbacks = dict()
258258
self.start_kernel()
259259
_thread.start_new_thread(self.process_messages, ())
260-
self.status_callback = None
261-
self.pager_callback = None
260+
self.status_callback = lambda x: None
262261
self.encoding = 'utf-8'
263262

264263
@property
@@ -280,11 +279,14 @@ def start_kernel(self):
280279
url = "http://" + self.baseurl + "/kernels?notebook=" + self.notebook_id
281280
req = urlopen(url, data=b"") # data="" makes it POST request
282281
req.read()
282+
self.create_websockets()
283283

284284
def restart_kernel(self):
285285
url = "http://" + self.baseurl + "/kernels/" + self.kernel_id + "/restart"
286286
req = urlopen(url, data=b"")
287287
req.read()
288+
self.create_websockets()
289+
self.status_callback("idle")
288290

289291
def interrupt_kernel(self):
290292
url = "http://" + self.baseurl + "/kernels/" + self.kernel_id + "/interrupt"
@@ -336,17 +338,17 @@ def register_callbacks(self, msg_id, output_callback,
336338
def process_messages(self):
337339
while True:
338340
m = self.message_queue.get()
339-
try:
340-
parent_id = m["parent_header"]["msg_id"]
341-
except:
342-
print(("No 'parent_header' in the message: " + str(m)))
343-
continue
344341
content = m["content"]
345342
msg_type = m["header"]["msg_type"]
346343

344+
if ("parent_header" in m) and ("msg_id" in m["parent_header"]):
345+
parent_id = m["parent_header"]["msg_id"]
346+
else:
347+
parent_id = None
348+
347349
if msg_type == "status":
348-
if self.status_callback:
349-
self.status_callback(content)
350+
if "execution_state" in content:
351+
self.status_callback(content["execution_state"])
350352

351353
elif parent_id in self.message_callbacks:
352354
callbacks = self.message_callbacks[parent_id]
@@ -383,17 +385,25 @@ def grab_output(msg_type, content):
383385
return grab_output
384386

385387
def create_websockets(self):
388+
if self.shell is not None:
389+
self.shell.close()
390+
391+
if self.iopub is not None:
392+
self.iopub.close()
393+
386394
url = "ws://" + self.baseurl + "/kernels/" + self.kernel_id + "/"
387395
self.shell = websocket.WebSocketApp(url=url + "shell",
388396
on_message=lambda ws, msg: self.on_shell_msg(msg),
389-
on_open=lambda ws: ws.send("hallo_shell"))
397+
on_open=lambda ws: ws.send(""),
398+
on_error=lambda ws, err: print(err))
390399
self.iopub = websocket.WebSocketApp(url=url + "iopub",
391400
on_message=lambda ws, msg: self.on_iopub_msg(msg),
392-
on_open=lambda ws: ws.send(""))
401+
on_open=lambda ws: ws.send(""),
402+
on_error=lambda ws, err: print(err))
393403

394404
_thread.start_new_thread(self.shell.run_forever, ())
395405
_thread.start_new_thread(self.iopub.run_forever, ())
396-
sleep(2)
406+
sleep(1)
397407
self.running = True
398408

399409
def create_message(self, msg_type, content):

ipy_view.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def rewrite_prompt_number(self, edit):
216216
def output_result(self, edit):
217217
output = self.cell.output
218218
output = "\n".join(map(lambda s: " " + s, output.splitlines()))
219-
print("output: '" + output + "'")
220219
self.write_to_region(edit, "inb_output", output)
221220

222221
def draw(self, edit):
@@ -292,20 +291,20 @@ def __init__(self, view, notebook_id, baseurl):
292291
self.notebook_id = notebook_id
293292
self.kernel = create_kernel(baseurl, notebook_id)
294293
self.kernel.status_callback = self.on_status
295-
self.on_status({"execution_state": "idle"})
294+
self.on_status("idle")
296295
self.notebook = self.kernel.get_notebook()
297296
self.modified = False
298297
self.show_modified_status(False)
299298

300-
view.set_name("IPy Notebook - " + self.notebook.name)
299+
self.set_name(self.notebook.name)
301300

302301

303302
def get_name(self):
304303
return self.notebook.name
305304

306305
def set_name(self, new_name):
307306
self.notebook.name = new_name
308-
view.set_name("IPy Notebook - " + self.notebook.name)
307+
self.view.set_name("IPy Notebook - " + self.notebook.name)
309308

310309
def get_cell_separator(self):
311310
return "\n\n"
@@ -472,10 +471,16 @@ def update_notebook_from_buffer(self):
472471
for cell in self.cells:
473472
cell.update_code()
474473

475-
def on_status(self, content):
474+
def restart_kernel(self):
475+
for cell in self.cells:
476+
if isinstance(cell, CodeCellView):
477+
cell.running = False
478+
self.kernel.restart_kernel()
479+
480+
481+
def on_status(self, execution_state):
476482
def set_status():
477-
status = "kernel: " + content["execution_state"]
478-
self.view.set_status("ExecutionStatus", status)
483+
self.view.set_status("ExecutionStatus", "kernel: " + execution_state)
479484
sublime.set_timeout(set_status, 0)
480485

481486
def handle_completions(self, view, prefix, locations):

subl_ipy_notebook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def run(self, edit, text):
7979
class InbRestartKernelCommand(sublime_plugin.TextCommand):
8080
def run(self, edit):
8181
nbview = manager.get_nb_view(self.view)
82-
if nbview and nbview.kernel:
83-
nbview.kernel.restart_kernel()
82+
if nbview:
83+
nbview.restart_kernel()
8484

8585

8686
class InbInterruptKernelCommand(sublime_plugin.TextCommand):

0 commit comments

Comments
 (0)