Skip to content

Commit 8ab657e

Browse files
committed
Update for jupyter_kernel_mgmt APIs
1 parent 7671727 commit 8ab657e

File tree

6 files changed

+53
-79
lines changed

6 files changed

+53
-79
lines changed

jupyter_ssh_kernels/__main__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
from jupyter_client.client2 import BlockingKernelClient2
2-
from jupyter_client.manager2 import shutdown
32
from .provider import SSHKernelProvider
43

54
# Try starting a remote kernel and connecting to it.
6-
km = SSHKernelProvider().launch('mydesktop')
5+
conn_info, km = SSHKernelProvider().launch('mydesktop')
76
print("Started remote kernel")
87
print()
9-
print(km.get_connection_info())
8+
print(conn_info)
109
print()
1110

12-
kc = BlockingKernelClient2(km.get_connection_info(), km)
11+
kc = BlockingKernelClient2(conn_info, km)
1312
print("Getting kernel info...")
1413
print(kc.kernel_info(reply=True)['content'])
1514
print()
1615

1716
import time
1817
time.sleep(5)
1918
print("Shutting down...")
20-
shutdown(kc, km)
19+
kc.shutdown_or_terminate()
2120
print("Shutdown complete")

jupyter_ssh_kernels/launcher.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
from jupyter_client.manager2 import KernelManager2ABC
3+
from paramiko import SSHClient
4+
from pathlib import Path
5+
import re
6+
7+
def remote_launch_py(kinfo):
8+
rl_path = Path(__file__).parent / 'remote_launch.py'
9+
with rl_path.open() as f:
10+
return f.read().format(**kinfo)
11+
12+
CONN_FILE_RE = r"!!!Started, connection_file: (.*) !!!"
13+
14+
def launch(server, remote_code):
15+
client = SSHClient()
16+
client.load_system_host_keys()
17+
client.connect(server)
18+
stdin, stdout, stderr = client.exec_command('python3')
19+
stdin.write(remote_code)
20+
stdin.close()
21+
stdin.channel.shutdown_write() # Sends EOF
22+
23+
for line in stdout:
24+
m = re.match(CONN_FILE_RE, line)
25+
if m:
26+
remote_conn_file = m.group(1)
27+
break
28+
else:
29+
print('Exit status', stdin.channel.exit_status)
30+
print("Stderr:")
31+
print(stderr.read().decode())
32+
print("Stdout:")
33+
print(stdout.read())
34+
raise RuntimeError("Remote kernel failed to start")
35+
36+
print("Remote connection file:", remote_conn_file)
37+
with client.open_sftp() as sftp:
38+
with sftp.open(remote_conn_file) as f:
39+
return json.load(f)

jupyter_ssh_kernels/manager.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

jupyter_ssh_kernels/provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
import pytoml
55

6-
from .manager import SSHKernelManager, remote_launch_py
6+
from .manager import launch, remote_launch_py
77

88
class SSHKernelProvider(KernelProviderBase):
99
id = "simple-ssh"
@@ -29,7 +29,7 @@ def find_kernels(self):
2929
def launch(self, name, cwd=None):
3030
for candidate_name, kinfo in self.find_kernels():
3131
if candidate_name == name:
32-
return SSHKernelManager(kinfo['address'],
33-
remote_launch_py(kinfo))
32+
conn_info = launch(kinfo['address'], remote_launch_py(kinfo))
33+
return (conn_info, None)
3434

3535
raise KeyError(name)

jupyter_ssh_kernels/remote_launch.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Sent to Python on the remote system to launch a kernel."""
22

3-
from jupyter_client.manager2 import KernelManager2
4-
km = KernelManager2(kernel_cmd={argv!r}, cwd={cwd!r}, ip={address!r})
5-
print("!!!Started, connection_file:", km.connection_file, '!!!', flush=True)
3+
from jupyter_kernel_mgmt.subproc import SubprocessKernelLauncher
4+
from jupyter_kernel_mgmt.nanny import KernelNanny
5+
l = SubprocessKernelLauncher(kernel_cmd={argv!r}, cwd={cwd!r}, ip={address!r})
6+
conn_info, mgr = l.launch()
7+
nanny = KernelNanny(conn_info, mgr)
8+
print("!!!Started, connection_file:", nanny.connection_file, '!!!', flush=True)
69
km.wait(None)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ home-page = "https://github.com/takluyver/jupyter_ssh_kernels"
1010
description-file = "README.rst"
1111
classifiers = ["License :: OSI Approved :: MIT License"]
1212

13-
[tool.flit.entrypoints."jupyter_client.kernel_providers"]
13+
[tool.flit.entrypoints."jupyter_kernel_mgmt.kernel_type_providers"]
1414
simple-ssh = "jupyter_ssh_kernels:SSHKernelProvider"

0 commit comments

Comments
 (0)