##// END OF EJS Templates
Make comm_manager a property of kernel, not shell
Thomas Kluyver -
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
@@ -18,6 +18,9 b' class Comm(LoggingConfigurable):'
18 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
18 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
19 allow_none=True)
19 allow_none=True)
20 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
20 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
21 def _kernel_default(self):
22 if Kernel.initialized():
23 return Kernel.instance()
21
24
22 iopub_socket = Any()
25 iopub_socket = Any()
23 def _iopub_socket_default(self):
26 def _iopub_socket_default(self):
@@ -55,16 +58,15 b' class Comm(LoggingConfigurable):'
55
58
56 def _publish_msg(self, msg_type, data=None, metadata=None, **keys):
59 def _publish_msg(self, msg_type, data=None, metadata=None, **keys):
57 """Helper for sending a comm message on IOPub"""
60 """Helper for sending a comm message on IOPub"""
58 if self.session is not None:
61 data = {} if data is None else data
59 data = {} if data is None else data
62 metadata = {} if metadata is None else metadata
60 metadata = {} if metadata is None else metadata
63 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
61 content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
64 self.session.send(self.iopub_socket, msg_type,
62 self.session.send(self.iopub_socket, msg_type,
65 content,
63 content,
66 metadata=json_clean(metadata),
64 metadata=json_clean(metadata),
67 parent=self.kernel._parent_header,
65 parent=self.kernel._parent_header,
68 ident=self.topic,
66 ident=self.topic,
69 )
67 )
68
70
69 def __del__(self):
71 def __del__(self):
70 """trigger close on gc"""
72 """trigger close on gc"""
@@ -76,10 +78,13 b' class Comm(LoggingConfigurable):'
76 """Open the frontend-side version of this comm"""
78 """Open the frontend-side version of this comm"""
77 if data is None:
79 if data is None:
78 data = self._open_data
80 data = self._open_data
81 comm_manager = getattr(self.kernel, 'comm_manager', None)
82 if comm_manager is None:
83 raise RuntimeError("Comms cannot be opened without a kernel "
84 "and a comm_manager attached to that kernel.")
85
86 comm_manager.register_comm(self)
79 self._closed = False
87 self._closed = False
80 ip = get_ipython()
81 if hasattr(ip, 'comm_manager'):
82 ip.comm_manager.register_comm(self)
83 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
88 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
84
89
85 def close(self, data=None, metadata=None):
90 def close(self, data=None, metadata=None):
@@ -90,9 +95,7 b' class Comm(LoggingConfigurable):'
90 if data is None:
95 if data is None:
91 data = self._close_data
96 data = self._close_data
92 self._publish_msg('comm_close', data, metadata)
97 self._publish_msg('comm_close', data, metadata)
93 ip = get_ipython()
98 self.kernel.comm_manager.unregister_comm(self)
94 if hasattr(ip, 'comm_manager'):
95 ip.comm_manager.unregister_comm(self)
96 self._closed = True
99 self._closed = True
97
100
98 def send(self, data=None, metadata=None):
101 def send(self, data=None, metadata=None):
@@ -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'
@@ -306,7 +306,7 b' class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,'
306 shell_stream = ZMQStream(self.shell_socket)
306 shell_stream = ZMQStream(self.shell_socket)
307 control_stream = ZMQStream(self.control_socket)
307 control_stream = ZMQStream(self.control_socket)
308
308
309 kernel_factory = self.kernel_class
309 kernel_factory = self.kernel_class.instance
310
310
311 kernel = kernel_factory(parent=self, session=self.session,
311 kernel = kernel_factory(parent=self, session=self.session,
312 shell_streams=[shell_stream, control_stream],
312 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 #-----------------------------------------------------------------------------
@@ -563,11 +562,6 b' class ZMQInteractiveShell(InteractiveShell):'
563 super(ZMQInteractiveShell, self).init_magics()
562 super(ZMQInteractiveShell, self).init_magics()
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
567 def init_comms(self):
568 self.comm_manager = CommManager(shell=self, parent=self,
569 kernel=self.kernel)
570 self.configurables.append(self.comm_manager)
571
565
572
566
573 InteractiveShellABC.register(ZMQInteractiveShell)
567 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now