Show More
@@ -1,11 +1,14 b'' | |||
|
1 | 1 | import signal |
|
2 | 2 | import sys |
|
3 | import uuid | |
|
4 | 3 | |
|
5 | 4 | from IPython.zmq.ipkernel import launch_kernel |
|
6 | 5 | from session import SessionManager |
|
7 | 6 | |
|
8 | 7 | |
|
8 | class DuplicateKernelError(Exception): | |
|
9 | pass | |
|
10 | ||
|
11 | ||
|
9 | 12 | class KernelManager(object): |
|
10 | 13 | |
|
11 | 14 | ip = '127.0.0.1' |
@@ -27,8 +30,9 b' class KernelManager(object):' | |||
|
27 | 30 | else: |
|
28 | 31 | return False |
|
29 | 32 | |
|
30 | def start_kernel(self): | |
|
31 | kid = str(uuid.uuid4()) | |
|
33 | def start_kernel(self, kernel_id): | |
|
34 | if kernel_id in self._kernels: | |
|
35 | raise DuplicateKernelError("Kernel already exists: %s" % kernel_id) | |
|
32 | 36 | (process, shell_port, iopub_port, stdin_port, hb_port) = launch_kernel() |
|
33 | 37 | d = dict( |
|
34 | 38 | process = process, |
@@ -36,10 +40,10 b' class KernelManager(object):' | |||
|
36 | 40 | iopub_port = iopub_port, |
|
37 | 41 | shell_port = shell_port, |
|
38 | 42 | hb_port = hb_port, |
|
39 | session_manager = SessionManager(self, kid, self.context) | |
|
43 | session_manager = SessionManager(self, kernel_id, self.context) | |
|
40 | 44 | ) |
|
41 | self._kernels[kid] = d | |
|
42 | return kid | |
|
45 | self._kernels[kernel_id] = d | |
|
46 | return kernel_id | |
|
43 | 47 | |
|
44 | 48 | def kill_kernel(self, kernel_id): |
|
45 | 49 | kernel_process = self.get_kernel_process(kernel_id) |
@@ -1,13 +1,11 b'' | |||
|
1 | 1 | import json |
|
2 | 2 | import logging |
|
3 | 3 | import os |
|
4 | import uuid | |
|
5 | 4 | |
|
6 | 5 | import zmq |
|
7 | 6 | |
|
8 | 7 | # Install the pyzmq ioloop. This has to be done before anything else from |
|
9 | 8 | # tornado is imported. |
|
10 | from zmq.eventloop.zmqstream import ZMQStream | |
|
11 | 9 | from zmq.eventloop import ioloop |
|
12 | 10 | import tornado.ioloop |
|
13 | 11 | tornado.ioloop = ioloop |
@@ -21,8 +19,8 b' from kernelmanager import KernelManager' | |||
|
21 | 19 | |
|
22 | 20 | options.define("port", default=8888, help="run on the given port", type=int) |
|
23 | 21 | |
|
24 |
_ |
|
|
25 |
_ |
|
|
22 | _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" | |
|
23 | _kernel_id_regex = r"(?P<kernel_id>\w+)" | |
|
26 | 24 | |
|
27 | 25 | |
|
28 | 26 | class MainHandler(web.RequestHandler): |
@@ -30,30 +28,44 b' class MainHandler(web.RequestHandler):' | |||
|
30 | 28 | self.render('notebook.html') |
|
31 | 29 | |
|
32 | 30 | |
|
33 |
class KernelHandler( |
|
|
31 | class BaseKernelHandler(object): | |
|
32 | ||
|
33 | def get_kernel(self): | |
|
34 | return self.application.kernel_manager | |
|
35 | ||
|
36 | def get_session(self, kernel_id): | |
|
37 | km = self.get_kernel() | |
|
38 | sm = km.get_session_manager(kernel_id) | |
|
39 | return sm | |
|
40 | ||
|
41 | ||
|
42 | class KernelHandler(web.RequestHandler, BaseKernelHandler): | |
|
34 | 43 | |
|
35 | 44 | def get(self): |
|
36 |
self.write(json.dumps(self. |
|
|
45 | self.write(json.dumps(self.get_kernel().kernel_ids)) | |
|
46 | ||
|
47 | def post(self, *args, **kwargs): | |
|
48 | kernel_id = kwargs['kernel_id'] | |
|
49 | self.get_kernel().start_kernel(kernel_id) | |
|
50 | logging.info("Starting kernel: %s" % kernel_id) | |
|
51 | self.write(json.dumps(kernel_id)) | |
|
37 | 52 | |
|
38 | def post(self): | |
|
39 | kid = self.application.kernel_manager.start_kernel() | |
|
40 | logging.info("Starting kernel: %s" % kid) | |
|
41 | self.write(json.dumps(kid)) | |
|
42 | 53 | |
|
54 | class SessionHandler(web.RequestHandler, BaseKernelHandler): | |
|
43 | 55 | |
|
44 | class SessionHandler(web.RequestHandler): | |
|
56 | def get(self, *args, **kwargs): | |
|
57 | kernel_id = kwargs['kernel_id'] | |
|
58 | self.write(json.dumps(self.get_session(kernel_id).session_ids)) | |
|
45 | 59 | |
|
46 | 60 | def post(self, *args, **kwargs): |
|
47 | 61 | kernel_id = kwargs['kernel_id'] |
|
48 | session_id = kwargs['session_id'] | |
|
62 | sm = self.get_session(kernel_id) | |
|
63 | session_id = sm.start_session() | |
|
49 | 64 | logging.info("Starting session: %s, %s" % (kernel_id,session_id)) |
|
50 | km = self.application.kernel_manager | |
|
51 | sm = km.get_session_manager(kernel_id) | |
|
52 | sm.start_session(session_id) | |
|
53 | self.finish() | |
|
65 | self.write(json.dumps(session_id)) | |
|
54 | 66 | |
|
55 | 67 | |
|
56 | class ZMQStreamHandler(websocket.WebSocketHandler): | |
|
68 | class ZMQStreamHandler(websocket.WebSocketHandler, BaseKernelHandler): | |
|
57 | 69 | |
|
58 | 70 | stream_name = '' |
|
59 | 71 | |
@@ -61,20 +73,17 b' class ZMQStreamHandler(websocket.WebSocketHandler):' | |||
|
61 | 73 | kernel_id = kwargs['kernel_id'] |
|
62 | 74 | session_id = kwargs['session_id'] |
|
63 | 75 | logging.info("Connection open: %s, %s" % (kernel_id,session_id)) |
|
64 |
sm = self. |
|
|
76 | sm = self.get_session(kernel_id) | |
|
65 | 77 | method_name = "get_%s_stream" % self.stream_name |
|
66 | 78 | method = getattr(sm, method_name) |
|
67 | 79 | self.zmq_stream = method(session_id) |
|
68 | 80 | self.zmq_stream.on_recv(self._on_zmq_reply) |
|
69 | self.session_manager = sm | |
|
70 | self.session_id = session_id | |
|
71 | 81 | |
|
72 | 82 | def on_message(self, msg): |
|
73 | 83 | logging.info("Message received: %r" % msg) |
|
74 | self.zmq_stream.send(msg) | |
|
84 | self.zmq_stream.send_unicode(msg) | |
|
75 | 85 | |
|
76 | 86 | def on_close(self): |
|
77 | logging.info("Connection closed: %s, %s" % (kernel_id,session_id)) | |
|
78 | 87 | self.zmq_stream.close() |
|
79 | 88 | |
|
80 | 89 | def _on_zmq_reply(self, msg): |
@@ -97,8 +106,8 b' class NotebookApplication(web.Application):' | |||
|
97 | 106 | def __init__(self): |
|
98 | 107 | handlers = [ |
|
99 | 108 | (r"/", MainHandler), |
|
100 | (r"/kernels", KernelHandler), | |
|
101 |
(r"/kernels/%s/sessions |
|
|
109 | (r"/kernels/%s" % (_kernel_id_regex,), KernelHandler), | |
|
110 | (r"/kernels/%s/sessions" % (_kernel_id_regex,), SessionHandler), | |
|
102 | 111 | (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler), |
|
103 | 112 | (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler), |
|
104 | 113 | ] |
@@ -1,4 +1,5 b'' | |||
|
1 | 1 | import logging |
|
2 | import uuid | |
|
2 | 3 | |
|
3 | 4 | import zmq |
|
4 | 5 | from zmq.eventloop.zmqstream import ZMQStream |
@@ -15,7 +16,21 b' class SessionManager(object):' | |||
|
15 | 16 | def __del__(self): |
|
16 | 17 | self.stop_all() |
|
17 | 18 | |
|
18 | def start_session(self, session_id): | |
|
19 | @property | |
|
20 | def session_ids(self): | |
|
21 | return self._session.keys() | |
|
22 | ||
|
23 | def __len__(self): | |
|
24 | return len(self.session_ids) | |
|
25 | ||
|
26 | def __contains__(self, session_id): | |
|
27 | if session_id in self.session_ids: | |
|
28 | return True | |
|
29 | else: | |
|
30 | return False | |
|
31 | ||
|
32 | def start_session(self): | |
|
33 | session_id = str(uuid.uuid4()) | |
|
19 | 34 | ports = self.kernel_manager.get_kernel_ports(self.kernel_id) |
|
20 | 35 | iopub_stream = self.create_connected_stream(ports['iopub_port'], zmq.SUB) |
|
21 | 36 | shell_stream = self.create_connected_stream(ports['shell_port'], zmq.XREQ) |
@@ -23,6 +38,7 b' class SessionManager(object):' | |||
|
23 | 38 | iopub_stream = iopub_stream, |
|
24 | 39 | shell_stream = shell_stream |
|
25 | 40 | ) |
|
41 | return session_id | |
|
26 | 42 | |
|
27 | 43 | def stop_session(self, session_id): |
|
28 | 44 | session_dict = self._sessions.get(session_id) |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now