Skip to content

Commit 63e969f

Browse files
committed
interrupt, shutdown and restart support in python
1 parent 6d1c756 commit 63e969f

1 file changed

Lines changed: 97 additions & 1 deletion

File tree

pythonFiles/PythonTools/ipythonServer.py

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@
6565
# Jupyter / IPython 4.x
6666
from jupyter_client import KernelManager
6767
from jupyter_client.kernelspec import KernelSpecManager
68+
from jupyter_client import MultiKernelManager
6869
kernelSpecManager = KernelSpecManager()
70+
multiKernelManager = MultiKernelManager()
6971
else:
7072
from IPython.kernel import KernelManager
7173
from IPython.kernel.kernelspec import KernelSpecManager
74+
from IPython.kernel.multikernelmanager import MultiKernelManager
7275
kernelSpecManager = KernelSpecManager()
76+
multiKernelManager = MultiKernelManager()
7377

7478
# End of the great "support IPython 2, 3, 4" strat
7579

@@ -126,6 +130,10 @@ class iPythonSocketServer(object):
126130
_LSKS = to_bytes('LSKS')
127131
_EROR = to_bytes('EROR')
128132
_TEST = to_bytes('TEST')
133+
_STRK = to_bytes('STRK')
134+
_STPK = to_bytes('STPK')
135+
_RSTK = to_bytes('RSTK')
136+
_ITPK = to_bytes('ITPK')
129137

130138
def __init__(self):
131139
import threading
@@ -189,7 +197,12 @@ def start_processing(self):
189197
else:
190198
cmd(self)
191199
except:
192-
self.replyWithError(cmd, id)
200+
commandName = utf_8.decode(inp)[0]
201+
try:
202+
commandName = ascii.Codec.encode(commandName)[0]
203+
except UnicodeEncodeError:
204+
pass
205+
self.replyWithError(commandName, id)
193206
else:
194207
if inp:
195208
print ('unknown command', inp)
@@ -258,6 +271,81 @@ def _cmd_lstk(self, id):
258271
write_string(self.conn, id)
259272
write_string(self.conn, kernelspecs)
260273

274+
def _cmd_strk(self, id):
275+
"""Start a kernel by name"""
276+
_debug_write('Listing kernel specs')
277+
while True:
278+
try:
279+
kernelName = read_string(self.conn)
280+
break
281+
except socket.timeout:
282+
pass
283+
kernelUUID = multiKernelManager.start_kernel(kernel_name=kernelName)
284+
# get the config and the connection FileExistsError
285+
kernel = multiKernelManager.get_kernel(kernelUUID)
286+
try:
287+
config = kernel.config
288+
except:
289+
config = {}
290+
try:
291+
connection_file = kernel.connection_file
292+
except:
293+
connection_file = ""
294+
295+
with self.send_lock:
296+
_debug_write('Replying with kernel Specs= ' + str(kernelUUID))
297+
write_bytes(self.conn, iPythonSocketServer._STRK)
298+
write_string(self.conn, id)
299+
write_string(self.conn, str(kernelUUID))
300+
write_string(self.conn, json.dumps(config))
301+
write_string(self.conn, connection_file)
302+
303+
def _cmd_stpk(self, id):
304+
"""Shutdown a kernel by UUID"""
305+
while True:
306+
try:
307+
kernelUUID = read_string(self.conn)
308+
break
309+
except socket.timeout:
310+
pass
311+
try:
312+
kernel = multiKernelManager.get_kernel(kernelUUID)
313+
kernel.shutdown_kernel()
314+
except:
315+
pass
316+
finally:
317+
with self.send_lock:
318+
write_bytes(self.conn, iPythonSocketServer._STPK)
319+
write_string(self.conn, id)
320+
321+
def _cmd_rstk(self, id):
322+
"""Restart a kernel by UUID"""
323+
while True:
324+
try:
325+
kernelUUID = read_string(self.conn)
326+
break
327+
except socket.timeout:
328+
pass
329+
kernel = multiKernelManager.get_kernel(kernelUUID)
330+
kernel.restart_kernel(now=True)
331+
with self.send_lock:
332+
write_bytes(self.conn, iPythonSocketServer._RSTK)
333+
write_string(self.conn, id)
334+
335+
def _cmd_itpk(self, id):
336+
"""Interrupt a kernel by UUID"""
337+
while True:
338+
try:
339+
kernelUUID = read_string(self.conn)
340+
break
341+
except socket.timeout:
342+
pass
343+
kernel = multiKernelManager.get_kernel(kernelUUID)
344+
kernel.interrupt_kernel()
345+
with self.send_lock:
346+
write_bytes(self.conn, iPythonSocketServer._ITPK)
347+
write_string(self.conn, id)
348+
261349
def _cmd_run(self):
262350
"""runs the received snippet of code"""
263351
# self.run_command(read_string(self.conn))
@@ -349,11 +437,19 @@ def flush(self):
349437
to_bytes('ping'): _cmd_ping,
350438
to_bytes('inpl'): _cmd_inpl,
351439
to_bytes('lsks'): _cmd_lstk,
440+
to_bytes('strk'): _cmd_strk,
441+
to_bytes('stpk'): _cmd_stpk,
442+
to_bytes('rstk'): _cmd_rstk,
443+
to_bytes('itpk'): _cmd_itpk,
352444
}
353445

354446
_COMMANDS_WITH_IDS = {
355447
to_bytes('lsks'): True,
356448
to_bytes('ping'): True,
449+
to_bytes('strk'): True,
450+
to_bytes('stpk'): True,
451+
to_bytes('rstk'): True,
452+
to_bytes('itpk'): True,
357453
}
358454

359455

0 commit comments

Comments
 (0)