##// END OF EJS Templates
Merge pull request #6540 from takluyver/comms-mgr-needs-no-shell...
Jonathan Frederic -
r18309:ddc05938 merge
parent child Browse files
Show More
@@ -523,7 +523,6 b' class InteractiveShell(SingletonConfigurable):'
523 self.init_pdb()
523 self.init_pdb()
524 self.init_extension_manager()
524 self.init_extension_manager()
525 self.init_payload()
525 self.init_payload()
526 self.init_comms()
527 self.hooks.late_startup_hook()
526 self.hooks.late_startup_hook()
528 self.events.trigger('shell_initialized', self)
527 self.events.trigger('shell_initialized', self)
529 atexit.register(self.atexit_operations)
528 atexit.register(self.atexit_operations)
@@ -2419,14 +2418,6 b' class InteractiveShell(SingletonConfigurable):'
2419 self.configurables.append(self.payload_manager)
2418 self.configurables.append(self.payload_manager)
2420
2419
2421 #-------------------------------------------------------------------------
2420 #-------------------------------------------------------------------------
2422 # Things related to widgets
2423 #-------------------------------------------------------------------------
2424
2425 def init_comms(self):
2426 # not implemented in the base class
2427 pass
2428
2429 #-------------------------------------------------------------------------
2430 # Things related to the prefilter
2421 # Things related to the prefilter
2431 #-------------------------------------------------------------------------
2422 #-------------------------------------------------------------------------
2432
2423
@@ -22,6 +22,9 b' from IPython.utils.py3compat import annotate'
22 class DummyComm(Comm):
22 class DummyComm(Comm):
23 comm_id = 'a-b-c-d'
23 comm_id = 'a-b-c-d'
24
24
25 def open(self, *args, **kwargs):
26 pass
27
25 def send(self, *args, **kwargs):
28 def send(self, *args, **kwargs):
26 pass
29 pass
27
30
@@ -6,7 +6,7 b''
6 import uuid
6 import uuid
7
7
8 from IPython.config import LoggingConfigurable
8 from IPython.config import LoggingConfigurable
9 from IPython.core.getipython import get_ipython
9 from IPython.kernel.zmq.kernelbase import Kernel
10
10
11 from IPython.utils.jsonutil import json_clean
11 from IPython.utils.jsonutil import json_clean
12 from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
12 from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
@@ -14,18 +14,21 b' from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any'
14
14
15 class Comm(LoggingConfigurable):
15 class Comm(LoggingConfigurable):
16
16
17 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
17 # If this is instantiated by a non-IPython kernel, shell will be None
18 def _shell_default(self):
18 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
19 return get_ipython()
19 allow_none=True)
20 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
21 def _kernel_default(self):
22 if Kernel.initialized():
23 return Kernel.instance()
20
24
21 iopub_socket = Any()
25 iopub_socket = Any()
22 def _iopub_socket_default(self):
26 def _iopub_socket_default(self):
23 return self.shell.kernel.iopub_socket
27 return self.kernel.iopub_socket
24 session = Instance('IPython.kernel.zmq.session.Session')
28 session = Instance('IPython.kernel.zmq.session.Session')
25 def _session_default(self):
29 def _session_default(self):
26 if self.shell is None or not hasattr(self.shell, 'kernel'):
30 if self.kernel is not None:
27 return
31 return self.kernel.session
28 return self.shell.kernel.session
29
32
30 target_name = Unicode('comm')
33 target_name = Unicode('comm')
31
34
@@ -56,14 +59,13 b' class Comm(LoggingConfigurable):'
56
59
57 def _publish_msg(self, msg_type, data=None, metadata=None, **keys):
60 def _publish_msg(self, msg_type, data=None, metadata=None, **keys):
58 """Helper for sending a comm message on IOPub"""
61 """Helper for sending a comm message on IOPub"""
59 if self.session is not None:
60 data = {} if data is None else data
62 data = {} if data is None else data
61 metadata = {} if metadata is None else metadata
63 metadata = {} if metadata is None else metadata
62 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
64 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
63 self.session.send(self.iopub_socket, msg_type,
65 self.session.send(self.iopub_socket, msg_type,
64 content,
66 content,
65 metadata=json_clean(metadata),
67 metadata=json_clean(metadata),
66 parent=self.shell.get_parent(),
68 parent=self.kernel._parent_header,
67 ident=self.topic,
69 ident=self.topic,
68 )
70 )
69
71
@@ -77,10 +79,13 b' class Comm(LoggingConfigurable):'
77 """Open the frontend-side version of this comm"""
79 """Open the frontend-side version of this comm"""
78 if data is None:
80 if data is None:
79 data = self._open_data
81 data = self._open_data
82 comm_manager = getattr(self.kernel, 'comm_manager', None)
83 if comm_manager is None:
84 raise RuntimeError("Comms cannot be opened without a kernel "
85 "and a comm_manager attached to that kernel.")
86
87 comm_manager.register_comm(self)
80 self._closed = False
88 self._closed = False
81 ip = get_ipython()
82 if hasattr(ip, 'comm_manager'):
83 ip.comm_manager.register_comm(self)
84 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
89 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
85
90
86 def close(self, data=None, metadata=None):
91 def close(self, data=None, metadata=None):
@@ -91,9 +96,7 b' class Comm(LoggingConfigurable):'
91 if data is None:
96 if data is None:
92 data = self._close_data
97 data = self._close_data
93 self._publish_msg('comm_close', data, metadata)
98 self._publish_msg('comm_close', data, metadata)
94 ip = get_ipython()
99 self.kernel.comm_manager.unregister_comm(self)
95 if hasattr(ip, 'comm_manager'):
96 ip.comm_manager.unregister_comm(self)
97 self._closed = True
100 self._closed = True
98
101
99 def send(self, data=None, metadata=None):
102 def send(self, data=None, metadata=None):
@@ -132,8 +135,10 b' class Comm(LoggingConfigurable):'
132 """Handle a comm_msg message"""
135 """Handle a comm_msg message"""
133 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
136 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
134 if self._msg_callback:
137 if self._msg_callback:
138 if self.shell:
135 self.shell.events.trigger('pre_execute')
139 self.shell.events.trigger('pre_execute')
136 self._msg_callback(msg)
140 self._msg_callback(msg)
141 if self.shell:
137 self.shell.events.trigger('post_execute')
142 self.shell.events.trigger('post_execute')
138
143
139
144
@@ -28,17 +28,17 b' def lazy_keys(dikt):'
28 class CommManager(LoggingConfigurable):
28 class CommManager(LoggingConfigurable):
29 """Manager for Comms in the Kernel"""
29 """Manager for Comms in the Kernel"""
30
30
31 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
31 # If this is instantiated by a non-IPython kernel, shell will be None
32 def _shell_default(self):
32 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
33 return get_ipython()
33 allow_none=True)
34 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
35
34 iopub_socket = Any()
36 iopub_socket = Any()
35 def _iopub_socket_default(self):
37 def _iopub_socket_default(self):
36 return self.shell.kernel.iopub_socket
38 return self.kernel.iopub_socket
37 session = Instance('IPython.kernel.zmq.session.Session')
39 session = Instance('IPython.kernel.zmq.session.Session')
38 def _session_default(self):
40 def _session_default(self):
39 if self.shell is None:
41 return self.kernel.session
40 return
41 return self.shell.kernel.session
42
42
43 comms = Dict()
43 comms = Dict()
44 targets = Dict()
44 targets = Dict()
@@ -68,6 +68,7 b' class CommManager(LoggingConfigurable):'
68 """Register a new comm"""
68 """Register a new comm"""
69 comm_id = comm.comm_id
69 comm_id = comm.comm_id
70 comm.shell = self.shell
70 comm.shell = self.shell
71 comm.kernel = self.kernel
71 comm.iopub_socket = self.iopub_socket
72 comm.iopub_socket = self.iopub_socket
72 self.comms[comm_id] = comm
73 self.comms[comm_id] = comm
73 return comm_id
74 return comm_id
@@ -102,6 +103,7 b' class CommManager(LoggingConfigurable):'
102 f = self.targets.get(target_name, None)
103 f = self.targets.get(target_name, None)
103 comm = Comm(comm_id=comm_id,
104 comm = Comm(comm_id=comm_id,
104 shell=self.shell,
105 shell=self.shell,
106 kernel=self.kernel,
105 iopub_socket=self.iopub_socket,
107 iopub_socket=self.iopub_socket,
106 primary=False,
108 primary=False,
107 )
109 )
@@ -10,6 +10,7 b' from IPython.utils.tokenutil import token_at_cursor'
10 from IPython.utils.traitlets import Instance, Type, Any
10 from IPython.utils.traitlets import Instance, Type, Any
11 from IPython.utils.decorators import undoc
11 from IPython.utils.decorators import undoc
12
12
13 from ..comm import CommManager
13 from .kernelbase import Kernel as KernelBase
14 from .kernelbase import Kernel as KernelBase
14 from .serialize import serialize_object, unpack_apply_message
15 from .serialize import serialize_object, unpack_apply_message
15 from .zmqshell import ZMQInteractiveShell
16 from .zmqshell import ZMQInteractiveShell
@@ -55,10 +56,12 b' class IPythonKernel(KernelBase):'
55 # TMP - hack while developing
56 # TMP - hack while developing
56 self.shell._reply_content = None
57 self.shell._reply_content = None
57
58
59 self.comm_manager = CommManager(shell=self.shell, parent=self,
60 kernel=self)
61 self.shell.configurables.append(self.comm_manager)
58 comm_msg_types = [ 'comm_open', 'comm_msg', 'comm_close' ]
62 comm_msg_types = [ 'comm_open', 'comm_msg', 'comm_close' ]
59 comm_manager = self.shell.comm_manager
60 for msg_type in comm_msg_types:
63 for msg_type in comm_msg_types:
61 self.shell_handlers[msg_type] = getattr(comm_manager, msg_type)
64 self.shell_handlers[msg_type] = getattr(self.comm_manager, msg_type)
62
65
63 # Kernel info fields
66 # Kernel info fields
64 implementation = 'ipython'
67 implementation = 'ipython'
@@ -302,7 +302,7 b' class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,'
302 shell_stream = ZMQStream(self.shell_socket)
302 shell_stream = ZMQStream(self.shell_socket)
303 control_stream = ZMQStream(self.control_socket)
303 control_stream = ZMQStream(self.control_socket)
304
304
305 kernel_factory = self.kernel_class
305 kernel_factory = self.kernel_class.instance
306
306
307 kernel = kernel_factory(parent=self, session=self.session,
307 kernel = kernel_factory(parent=self, session=self.session,
308 shell_streams=[shell_stream, control_stream],
308 shell_streams=[shell_stream, control_stream],
@@ -19,7 +19,7 b' import zmq'
19 from zmq.eventloop import ioloop
19 from zmq.eventloop import ioloop
20 from zmq.eventloop.zmqstream import ZMQStream
20 from zmq.eventloop.zmqstream import ZMQStream
21
21
22 from IPython.config.configurable import Configurable
22 from IPython.config.configurable import SingletonConfigurable
23 from IPython.core.error import StdinNotImplementedError
23 from IPython.core.error import StdinNotImplementedError
24 from IPython.core import release
24 from IPython.core import release
25 from IPython.utils import py3compat
25 from IPython.utils import py3compat
@@ -32,7 +32,7 b' from IPython.utils.traitlets import ('
32 from .session import Session
32 from .session import Session
33
33
34
34
35 class Kernel(Configurable):
35 class Kernel(SingletonConfigurable):
36
36
37 #---------------------------------------------------------------------------
37 #---------------------------------------------------------------------------
38 # Kernel interface
38 # Kernel interface
@@ -49,7 +49,6 b' from IPython.utils.warn import error'
49 from IPython.kernel.zmq.displayhook import ZMQShellDisplayHook
49 from IPython.kernel.zmq.displayhook import ZMQShellDisplayHook
50 from IPython.kernel.zmq.datapub import ZMQDataPublisher
50 from IPython.kernel.zmq.datapub import ZMQDataPublisher
51 from IPython.kernel.zmq.session import extract_header
51 from IPython.kernel.zmq.session import extract_header
52 from IPython.kernel.comm import CommManager
53 from .session import Session
52 from .session import Session
54
53
55 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
@@ -564,9 +563,5 b' class ZMQInteractiveShell(InteractiveShell):'
564 self.register_magics(KernelMagics)
563 self.register_magics(KernelMagics)
565 self.magics_manager.register_alias('ed', 'edit')
564 self.magics_manager.register_alias('ed', 'edit')
566
565
567 def init_comms(self):
568 self.comm_manager = CommManager(shell=self, parent=self)
569 self.configurables.append(self.comm_manager)
570
571
566
572 InteractiveShellABC.register(ZMQInteractiveShell)
567 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now