##// END OF EJS Templates
Collapse inprocess channel classes together
Thomas Kluyver -
Show More
@@ -1,7 +1,5 b''
1 from .channels import (
1 from .channels import (
2 InProcessShellChannel,
2 InProcessChannel,
3 InProcessIOPubChannel,
4 InProcessStdInChannel,
5 InProcessHBChannel,
3 InProcessHBChannel,
6 )
4 )
7
5
@@ -21,16 +21,14 b' from IPython.utils.traitlets import Type'
21
21
22 # Local imports
22 # Local imports
23 from .channels import (
23 from .channels import (
24 InProcessShellChannel,
24 InProcessChannel,
25 InProcessIOPubChannel,
26 InProcessStdInChannel,
27 )
25 )
28 from .client import InProcessKernelClient
26 from .client import InProcessKernelClient
29
27
30 class BlockingChannelMixin(object):
28 class BlockingInProcessChannel(InProcessChannel):
31
29
32 def __init__(self, *args, **kwds):
30 def __init__(self, *args, **kwds):
33 super(BlockingChannelMixin, self).__init__(*args, **kwds)
31 super(BlockingInProcessChannel, self).__init__(*args, **kwds)
34 self._in_queue = Queue()
32 self._in_queue = Queue()
35
33
36 def call_handlers(self, msg):
34 def call_handlers(self, msg):
@@ -58,14 +56,8 b' class BlockingChannelMixin(object):'
58 """ Is there a message that has been received? """
56 """ Is there a message that has been received? """
59 return not self._in_queue.empty()
57 return not self._in_queue.empty()
60
58
61 class BlockingInProcessShellChannel(BlockingChannelMixin, InProcessShellChannel):
62 pass
63
64 class BlockingInProcessIOPubChannel(BlockingChannelMixin, InProcessIOPubChannel):
65 pass
66
67 class BlockingInProcessStdInChannel(BlockingChannelMixin, InProcessStdInChannel):
68
59
60 class BlockingInProcessStdInChannel(BlockingInProcessChannel):
69 def call_handlers(self, msg):
61 def call_handlers(self, msg):
70 """ Overridden for the in-process channel.
62 """ Overridden for the in-process channel.
71
63
@@ -76,11 +68,27 b' class BlockingInProcessStdInChannel(BlockingChannelMixin, InProcessStdInChannel)'
76 _raw_input = self.client.kernel._sys_raw_input
68 _raw_input = self.client.kernel._sys_raw_input
77 prompt = msg['content']['prompt']
69 prompt = msg['content']['prompt']
78 raw_print(prompt, end='')
70 raw_print(prompt, end='')
79 self.input(_raw_input())
71 self.client.input(_raw_input())
80
72
81 class BlockingInProcessKernelClient(InProcessKernelClient):
73 class BlockingInProcessKernelClient(InProcessKernelClient):
82
74
83 # The classes to use for the various channels.
75 # The classes to use for the various channels.
84 shell_channel_class = Type(BlockingInProcessShellChannel)
76 shell_channel_class = Type(BlockingInProcessChannel)
85 iopub_channel_class = Type(BlockingInProcessIOPubChannel)
77 iopub_channel_class = Type(BlockingInProcessChannel)
86 stdin_channel_class = Type(BlockingInProcessStdInChannel)
78 stdin_channel_class = Type(BlockingInProcessStdInChannel)
79
80 def wait_for_ready(self):
81 # Wait for kernel info reply on shell channel
82 while True:
83 msg = self.shell_channel.get_msg(block=True)
84 if msg['msg_type'] == 'kernel_info_reply':
85 self._handle_kernel_info_reply(msg)
86 break
87
88 # Flush IOPub channel
89 while True:
90 try:
91 msg = self.iopub_channel.get_msg(block=True, timeout=0.2)
92 print(msg['msg_type'])
93 except Empty:
94 break
@@ -23,10 +23,6 b' class InProcessChannel(object):'
23 self.client = client
23 self.client = client
24 self._is_alive = False
24 self._is_alive = False
25
25
26 #--------------------------------------------------------------------------
27 # Channel interface
28 #--------------------------------------------------------------------------
29
30 def is_alive(self):
26 def is_alive(self):
31 return self._is_alive
27 return self._is_alive
32
28
@@ -46,9 +42,6 b' class InProcessChannel(object):'
46 def flush(self, timeout=1.0):
42 def flush(self, timeout=1.0):
47 pass
43 pass
48
44
49 #--------------------------------------------------------------------------
50 # InProcessChannel interface
51 #--------------------------------------------------------------------------
52
45
53 def call_handlers_later(self, *args, **kwds):
46 def call_handlers_later(self, *args, **kwds):
54 """ Call the message handlers later.
47 """ Call the message handlers later.
@@ -68,20 +61,6 b' class InProcessChannel(object):'
68 raise NotImplementedError
61 raise NotImplementedError
69
62
70
63
71 class InProcessShellChannel(InProcessChannel):
72 """See `IPython.kernel.channels.ShellChannel` for docstrings."""
73
74 # flag for whether execute requests should be allowed to call raw_input
75 allow_stdin = True
76
77 class InProcessIOPubChannel(InProcessChannel):
78 """See `IPython.kernel.channels.IOPubChannel` for docstrings."""
79
80
81 class InProcessStdInChannel(InProcessChannel):
82 """See `IPython.kernel.channels.StdInChannel` for docstrings."""
83
84
85
64
86 class InProcessHBChannel(InProcessChannel):
65 class InProcessHBChannel(InProcessChannel):
87 """See `IPython.kernel.channels.HBChannel` for docstrings."""
66 """See `IPython.kernel.channels.HBChannel` for docstrings."""
@@ -101,11 +80,5 b' class InProcessHBChannel(InProcessChannel):'
101 def is_beating(self):
80 def is_beating(self):
102 return not self._pause
81 return not self._pause
103
82
104 #-----------------------------------------------------------------------------
105 # ABC Registration
106 #-----------------------------------------------------------------------------
107
83
108 ShellChannelABC.register(InProcessShellChannel)
109 IOPubChannelABC.register(InProcessIOPubChannel)
110 HBChannelABC.register(InProcessHBChannel)
84 HBChannelABC.register(InProcessHBChannel)
111 StdInChannelABC.register(InProcessStdInChannel)
@@ -19,10 +19,8 b' from IPython.kernel.client import KernelClient'
19
19
20 # Local imports
20 # Local imports
21 from .channels import (
21 from .channels import (
22 InProcessShellChannel,
22 InProcessChannel,
23 InProcessIOPubChannel,
24 InProcessHBChannel,
23 InProcessHBChannel,
25 InProcessStdInChannel,
26
24
27 )
25 )
28
26
@@ -41,9 +39,9 b' class InProcessKernelClient(KernelClient):'
41 """
39 """
42
40
43 # The classes to use for the various channels.
41 # The classes to use for the various channels.
44 shell_channel_class = Type(InProcessShellChannel)
42 shell_channel_class = Type(InProcessChannel)
45 iopub_channel_class = Type(InProcessIOPubChannel)
43 iopub_channel_class = Type(InProcessChannel)
46 stdin_channel_class = Type(InProcessStdInChannel)
44 stdin_channel_class = Type(InProcessChannel)
47 hb_channel_class = Type(InProcessHBChannel)
45 hb_channel_class = Type(InProcessHBChannel)
48
46
49 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
47 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
@@ -26,6 +26,7 b' class InProcessKernelTestCase(unittest.TestCase):'
26 self.km.start_kernel()
26 self.km.start_kernel()
27 self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel)
27 self.kc = BlockingInProcessKernelClient(kernel=self.km.kernel)
28 self.kc.start_channels()
28 self.kc.start_channels()
29 self.kc.wait_for_ready()
29
30
30 @skipif_not_matplotlib
31 @skipif_not_matplotlib
31 def test_pylab(self):
32 def test_pylab(self):
@@ -51,6 +51,7 b' class InProcessKernelManagerTestCase(unittest.TestCase):'
51 km.start_kernel()
51 km.start_kernel()
52 kc = BlockingInProcessKernelClient(kernel=km.kernel)
52 kc = BlockingInProcessKernelClient(kernel=km.kernel)
53 kc.start_channels()
53 kc.start_channels()
54 kc.wait_for_ready()
54 kc.execute('foo = 1')
55 kc.execute('foo = 1')
55 self.assertEquals(km.kernel.shell.user_ns['foo'], 1)
56 self.assertEquals(km.kernel.shell.user_ns['foo'], 1)
56
57
@@ -61,6 +62,7 b' class InProcessKernelManagerTestCase(unittest.TestCase):'
61 km.start_kernel()
62 km.start_kernel()
62 kc = BlockingInProcessKernelClient(kernel=km.kernel)
63 kc = BlockingInProcessKernelClient(kernel=km.kernel)
63 kc.start_channels()
64 kc.start_channels()
65 kc.wait_for_ready()
64 km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
66 km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
65 kc.complete('my_ba', 5)
67 kc.complete('my_ba', 5)
66 msg = kc.get_shell_msg()
68 msg = kc.get_shell_msg()
@@ -75,6 +77,7 b' class InProcessKernelManagerTestCase(unittest.TestCase):'
75 km.start_kernel()
77 km.start_kernel()
76 kc = BlockingInProcessKernelClient(kernel=km.kernel)
78 kc = BlockingInProcessKernelClient(kernel=km.kernel)
77 kc.start_channels()
79 kc.start_channels()
80 kc.wait_for_ready()
78 km.kernel.shell.user_ns['foo'] = 1
81 km.kernel.shell.user_ns['foo'] = 1
79 kc.inspect('foo')
82 kc.inspect('foo')
80 msg = kc.get_shell_msg()
83 msg = kc.get_shell_msg()
@@ -91,6 +94,7 b' class InProcessKernelManagerTestCase(unittest.TestCase):'
91 km.start_kernel()
94 km.start_kernel()
92 kc = BlockingInProcessKernelClient(kernel=km.kernel)
95 kc = BlockingInProcessKernelClient(kernel=km.kernel)
93 kc.start_channels()
96 kc.start_channels()
97 kc.wait_for_ready()
94 kc.execute('%who')
98 kc.execute('%who')
95 kc.history(hist_access_type='tail', n=1)
99 kc.history(hist_access_type='tail', n=1)
96 msg = kc.shell_channel.get_msgs()[-1]
100 msg = kc.shell_channel.get_msgs()[-1]
General Comments 0
You need to be logged in to leave comments. Login now