Show More
@@ -1,259 +1,253 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Frontend of ipython working with python-zmq |
|
2 | """Frontend of ipython working with python-zmq | |
3 |
|
3 | |||
4 | Ipython's frontend, is a ipython interface that send request to kernel and proccess the kernel's outputs. |
|
4 | Ipython's frontend, is a ipython interface that send request to kernel and proccess the kernel's outputs. | |
5 |
|
5 | |||
6 | For more details, see the ipython-zmq design |
|
6 | For more details, see the ipython-zmq design | |
7 | """ |
|
7 | """ | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2010 The IPython Development Team |
|
9 | # Copyright (C) 2010 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | from __future__ import print_function |
|
18 | from __future__ import print_function | |
19 |
|
19 | |||
20 | import __builtin__ |
|
20 | import __builtin__ | |
21 | import sys |
|
21 | import sys | |
22 | import os |
|
22 | import os | |
23 | from Queue import Empty |
|
23 | from Queue import Empty | |
24 | import readline |
|
24 | import readline | |
25 | import rlcompleter |
|
25 | import rlcompleter | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Imports from ipython |
|
28 | # Imports from ipython | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | from IPython.external.argparse import ArgumentParser |
|
30 | from IPython.external.argparse import ArgumentParser | |
31 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
31 | from IPython.core.inputsplitter import IPythonInputSplitter | |
32 | from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager |
|
32 | from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager | |
33 | from IPython.frontend.zmqterminal.completer import ClientCompleter2p |
|
33 | from IPython.frontend.zmqterminal.completer import ClientCompleter2p | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Network Constants |
|
36 | # Network Constants | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS |
|
39 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | |
40 | class Frontend(object): |
|
40 | class Frontend(object): | |
41 | """This class is a simple frontend to ipython-zmq |
|
41 | """This class is a simple frontend to ipython-zmq | |
42 |
|
42 | |||
43 | NOTE: this class uses kernelmanager to manipulate sockets |
|
43 | NOTE: this class uses kernelmanager to manipulate sockets | |
44 |
|
44 | |||
45 | Parameters: |
|
45 | Parameters: | |
46 | ----------- |
|
46 | ----------- | |
47 | kernelmanager : object |
|
47 | kernelmanager : object | |
48 | instantiated object from class KernelManager in module kernelmanager |
|
48 | instantiated object from class KernelManager in module kernelmanager | |
49 |
|
49 | |||
50 | """ |
|
50 | """ | |
51 |
|
51 | |||
52 | def __init__(self, kernelmanager): |
|
52 | def __init__(self, kernelmanager): | |
53 | self.km = kernelmanager |
|
53 | self.km = kernelmanager | |
54 | self.session_id = self.km.session.session |
|
54 | self.session_id = self.km.session.session | |
55 | self.completer = ClientCompleter2p(self, self.km) |
|
55 | self.completer = ClientCompleter2p(self, self.km) | |
56 | readline.parse_and_bind("tab: complete") |
|
56 | readline.parse_and_bind("tab: complete") | |
57 | readline.parse_and_bind('set show-all-if-ambiguous on') |
|
57 | readline.parse_and_bind('set show-all-if-ambiguous on') | |
58 | readline.set_completer(self.completer.complete) |
|
58 | readline.set_completer(self.completer.complete) | |
59 |
|
59 | |||
60 | history_path = os.path.expanduser('~/.ipython/history') |
|
60 | history_path = os.path.expanduser('~/.ipython/history') | |
61 | if os.path.isfile(history_path): |
|
61 | if os.path.isfile(history_path): | |
62 | rlcompleter.readline.read_history_file(history_path) |
|
62 | rlcompleter.readline.read_history_file(history_path) | |
63 | else: |
|
63 | else: | |
64 | print("history file cannot be read.") |
|
64 | print("history file cannot be read.") | |
65 |
|
65 | |||
66 | self.messages = {} |
|
66 | self.messages = {} | |
67 |
|
67 | |||
68 | self._splitter = IPythonInputSplitter() |
|
68 | self._splitter = IPythonInputSplitter() | |
69 | self.code = "" |
|
69 | self.code = "" | |
70 |
|
70 | |||
71 | self.prompt_count = 0 |
|
71 | self.prompt_count = 0 | |
72 | self._get_initial_prompt() |
|
72 | self._get_initial_prompt() | |
73 |
|
73 | |||
74 | def _get_initial_prompt(self): |
|
74 | def _get_initial_prompt(self): | |
75 | self._execute('', hidden=True) |
|
75 | self._execute('', hidden=True) | |
76 |
|
76 | |||
77 | def interact(self): |
|
77 | def interact(self): | |
78 | """Gets input from console using inputsplitter, then |
|
78 | """Gets input from console using inputsplitter, then | |
79 | while you enter code it can indent and set index id to any input |
|
79 | while you enter code it can indent and set index id to any input | |
80 | """ |
|
80 | """ | |
81 |
|
81 | |||
82 | try: |
|
82 | try: | |
83 | print() |
|
83 | print() | |
84 |
self._splitter.push(raw_input(' |
|
84 | self._splitter.push(raw_input('In [%i]: '%self.prompt_count+self.code)) | |
85 | while self._splitter.push_accepts_more(): |
|
85 | while self._splitter.push_accepts_more(): | |
86 |
self.code = raw_input(' |
|
86 | self.code = raw_input('.....: '+' '*self._splitter.indent_spaces) | |
87 | self._splitter.push(' '*self._splitter.indent_spaces+self.code) |
|
87 | self._splitter.push(' '*self._splitter.indent_spaces+self.code) | |
88 | self._execute(self._splitter.source,False) |
|
88 | self._execute(self._splitter.source,False) | |
89 | self._splitter.reset() |
|
89 | self._splitter.reset() | |
90 | except KeyboardInterrupt: |
|
90 | except KeyboardInterrupt: | |
91 | print('\nKeyboardInterrupt\n') |
|
91 | print('\nKeyboardInterrupt\n') | |
92 | pass |
|
92 | pass | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def start(self): |
|
95 | def start(self): | |
96 | """Start the interaction loop, calling the .interact() method for each |
|
96 | """Start the interaction loop, calling the .interact() method for each | |
97 | input cell. |
|
97 | input cell. | |
98 | """ |
|
98 | """ | |
99 | while True: |
|
99 | while True: | |
100 | try: |
|
100 | try: | |
101 | self.interact() |
|
101 | self.interact() | |
102 | except KeyboardInterrupt: |
|
102 | except KeyboardInterrupt: | |
103 | print('\nKeyboardInterrupt\n') |
|
103 | print('\nKeyboardInterrupt\n') | |
104 | pass |
|
104 | pass | |
105 | except EOFError: |
|
105 | except EOFError: | |
106 | answer = '' |
|
106 | answer = '' | |
107 | while True: |
|
107 | while True: | |
108 | answer = raw_input('\nDo you really want to exit ([y]/n)?') |
|
108 | answer = raw_input('\nDo you really want to exit ([y]/n)?') | |
109 | if answer == 'y' or answer == '' : |
|
109 | if answer == 'y' or answer == '' : | |
110 | self.km.shutdown_kernel() |
|
110 | self.km.shutdown_kernel() | |
111 | sys.exit() |
|
111 | sys.exit() | |
112 | elif answer == 'n': |
|
112 | elif answer == 'n': | |
113 | break |
|
113 | break | |
114 |
|
114 | |||
115 | def _execute(self, source, hidden = True): |
|
115 | def _execute(self, source, hidden = True): | |
116 | """ Execute 'source'. If 'hidden', do not show any output. |
|
116 | """ Execute 'source'. If 'hidden', do not show any output. | |
117 |
|
117 | |||
118 | See parent class :meth:`execute` docstring for full details. |
|
118 | See parent class :meth:`execute` docstring for full details. | |
119 | """ |
|
119 | """ | |
120 |
msg_id = self.km. |
|
120 | msg_id = self.km.shell_channel.execute(source, hidden) | |
121 |
while not self.km. |
|
121 | while not self.km.shell_channel.msg_ready(): | |
122 | try: |
|
122 | try: | |
123 |
self.handle_ |
|
123 | self.handle_stdin_channel(timeout=0.1) | |
124 | except Empty: |
|
124 | except Empty: | |
125 | pass |
|
125 | pass | |
126 | self.handle_execute_reply(msg_id) |
|
126 | self.handle_execute_reply(msg_id) | |
127 |
|
127 | |||
128 | def handle_execute_reply(self, msg_id): |
|
128 | def handle_execute_reply(self, msg_id): | |
129 |
msg |
|
129 | msg = self.km.shell_channel.get_msg() | |
130 |
if msg |
|
130 | if msg["parent_header"]["msg_id"] == msg_id: | |
131 |
if msg |
|
131 | if msg["content"]["status"] == 'ok' : | |
132 | self.handle_sub_channel() |
|
132 | self.handle_sub_channel() | |
133 |
|
133 | |||
134 |
elif msg |
|
134 | elif msg["content"]["status"] == 'error': | |
135 |
for frame in msg |
|
135 | for frame in msg["content"]["traceback"]: | |
136 | print(frame, file=sys.stderr) |
|
136 | print(frame, file=sys.stderr) | |
137 |
|
137 | |||
138 |
self.prompt_count = msg |
|
138 | self.prompt_count = msg["content"]["execution_count"] + 1 | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | def handle_sub_channel(self): |
|
141 | def handle_sub_channel(self): | |
142 | """ Method to procces subscribe channel's messages |
|
142 | """ Method to procces subscribe channel's messages | |
143 |
|
143 | |||
144 | This method reads a message and processes the content in different |
|
144 | This method reads a message and processes the content in different | |
145 | outputs like stdout, stderr, pyout and status |
|
145 | outputs like stdout, stderr, pyout and status | |
146 |
|
146 | |||
147 | Arguments: |
|
147 | Arguments: | |
148 | sub_msg: message receive from kernel in the sub socket channel |
|
148 | sub_msg: message receive from kernel in the sub socket channel | |
149 | capture by kernel manager. |
|
149 | capture by kernel manager. | |
150 | """ |
|
150 | """ | |
151 | while self.km.sub_channel.msg_ready(): |
|
151 | while self.km.sub_channel.msg_ready(): | |
152 | sub_msg = self.km.sub_channel.get_msg() |
|
152 | sub_msg = self.km.sub_channel.get_msg() | |
153 | if self.session_id == sub_msg['parent_header']['session']: |
|
153 | if self.session_id == sub_msg['parent_header']['session']: | |
154 | if sub_msg['msg_type'] == 'status' : |
|
154 | if sub_msg['msg_type'] == 'status' : | |
155 | if sub_msg["content"]["execution_state"] == "busy" : |
|
155 | if sub_msg["content"]["execution_state"] == "busy" : | |
156 | pass |
|
156 | pass | |
157 |
|
157 | |||
158 | elif sub_msg['msg_type'] == 'stream' : |
|
158 | elif sub_msg['msg_type'] == 'stream' : | |
159 | if sub_msg["content"]["name"] == "stdout": |
|
159 | if sub_msg["content"]["name"] == "stdout": | |
160 | print(sub_msg["content"]["data"], file=sys.stdout, end="") |
|
160 | print(sub_msg["content"]["data"], file=sys.stdout, end="") | |
161 | sys.stdout.flush() |
|
161 | sys.stdout.flush() | |
162 | elif sub_msg["content"]["name"] == "stderr" : |
|
162 | elif sub_msg["content"]["name"] == "stderr" : | |
163 | print(sub_msg["content"]["data"], file=sys.stderr, end="") |
|
163 | print(sub_msg["content"]["data"], file=sys.stderr, end="") | |
164 | sys.stderr.flush() |
|
164 | sys.stderr.flush() | |
165 |
|
165 | |||
166 | elif sub_msg['msg_type'] == 'pyout' : |
|
166 | elif sub_msg['msg_type'] == 'pyout' : | |
167 | print("Out[%i]:"%sub_msg["content"]["execution_count"], |
|
167 | print("Out[%i]:"%sub_msg["content"]["execution_count"], | |
168 | sub_msg["content"]["data"]["text/plain"], |
|
168 | sub_msg["content"]["data"]["text/plain"], | |
169 | file=sys.stdout) |
|
169 | file=sys.stdout) | |
170 | sys.stdout.flush() |
|
170 | sys.stdout.flush() | |
171 |
|
171 | |||
172 |
def handle_ |
|
172 | def handle_stdin_channel(self, timeout=0.1): | |
173 | """ Method to capture raw_input |
|
173 | """ Method to capture raw_input | |
174 | """ |
|
174 | """ | |
175 |
msg_rep = self.km. |
|
175 | msg_rep = self.km.stdin_channel.get_msg(timeout=timeout) | |
176 | if self.session_id == msg_rep["parent_header"]["session"] : |
|
176 | if self.session_id == msg_rep["parent_header"]["session"] : | |
177 | raw_data = raw_input(msg_rep["content"]["prompt"]) |
|
177 | raw_data = raw_input(msg_rep["content"]["prompt"]) | |
178 |
self.km. |
|
178 | self.km.stdin_channel.input(raw_data) | |
179 |
|
179 | |||
180 |
|
180 | |||
181 |
|
181 | |||
182 |
|
182 | |||
183 | def start_frontend(): |
|
183 | def start_frontend(): | |
184 | """ Entry point for application. |
|
184 | """ Entry point for application. | |
185 |
|
185 | |||
186 | """ |
|
186 | """ | |
187 | # Parse command line arguments. |
|
187 | # Parse command line arguments. | |
188 | parser = ArgumentParser() |
|
188 | parser = ArgumentParser() | |
189 | kgroup = parser.add_argument_group('kernel options') |
|
189 | kgroup = parser.add_argument_group('kernel options') | |
190 | kgroup.add_argument('-e', '--existing', action='store_true', |
|
190 | kgroup.add_argument('-e', '--existing', action='store_true', | |
191 | help='connect to an existing kernel') |
|
191 | help='connect to an existing kernel') | |
192 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, |
|
192 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, | |
193 | help=\ |
|
193 | help=\ | |
194 | "set the kernel\'s IP address [default localhost].\ |
|
194 | "set the kernel\'s IP address [default localhost].\ | |
195 | If the IP address is something other than localhost, then \ |
|
195 | If the IP address is something other than localhost, then \ | |
196 | Consoles on other machines will be able to connect\ |
|
196 | Consoles on other machines will be able to connect\ | |
197 | to the Kernel, so be careful!") |
|
197 | to the Kernel, so be careful!") | |
198 |
kgroup.add_argument('-- |
|
198 | kgroup.add_argument('--shell', type=int, metavar='PORT', default=0, | |
199 | help='set the XREQ channel port [default random]') |
|
199 | help='set the XREQ channel port [default random]') | |
200 |
kgroup.add_argument('-- |
|
200 | kgroup.add_argument('--iopub', type=int, metavar='PORT', default=0, | |
201 | help='set the SUB channel port [default random]') |
|
201 | help='set the SUB channel port [default random]') | |
202 |
kgroup.add_argument('-- |
|
202 | kgroup.add_argument('--stdin', type=int, metavar='PORT', default=0, | |
203 | help='set the REP channel port [default random]') |
|
203 | help='set the REP channel port [default random]') | |
204 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, |
|
204 | kgroup.add_argument('--hb', type=int, metavar='PORT', default=0, | |
205 | help='set the heartbeat port [default random]') |
|
205 | help='set the heartbeat port [default random]') | |
206 |
|
206 | |||
207 | egroup = kgroup.add_mutually_exclusive_group() |
|
207 | egroup = kgroup.add_mutually_exclusive_group() | |
208 | egroup.add_argument('--pure', action='store_true', help = \ |
|
208 | egroup.add_argument('--pure', action='store_true', help = \ | |
209 | 'use a pure Python kernel instead of an IPython kernel') |
|
209 | 'use a pure Python kernel instead of an IPython kernel') | |
210 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
210 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | |
211 | const='auto', help = \ |
|
211 | const='auto', help = \ | |
212 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
212 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | |
213 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
213 | given, the GUI backend is matplotlib's, otherwise use one of: \ | |
214 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") |
|
214 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") | |
215 | egroup.add_argument('--colors', type=str, |
|
215 | egroup.add_argument('--colors', type=str, | |
216 | help="Set the color scheme (LightBG,Linux,NoColor). This is guessed\ |
|
216 | help="Set the color scheme (LightBG,Linux,NoColor). This is guessed\ | |
217 | based on the pygments style if not set.") |
|
217 | based on the pygments style if not set.") | |
218 |
|
218 | |||
219 | args = parser.parse_args() |
|
219 | args = parser.parse_args() | |
220 |
|
220 | |||
221 | # parse the colors arg down to current known labels |
|
221 | # parse the colors arg down to current known labels | |
222 | if args.colors: |
|
222 | if args.colors: | |
223 | colors=args.colors.lower() |
|
223 | colors=args.colors.lower() | |
224 | if colors in ('lightbg', 'light'): |
|
224 | if colors in ('lightbg', 'light'): | |
225 | colors='lightbg' |
|
225 | colors='lightbg' | |
226 | elif colors in ('dark', 'linux'): |
|
226 | elif colors in ('dark', 'linux'): | |
227 | colors='linux' |
|
227 | colors='linux' | |
228 | else: |
|
228 | else: | |
229 | colors='nocolor' |
|
229 | colors='nocolor' | |
230 | else: |
|
230 | else: | |
231 | colors=None |
|
231 | colors=None | |
232 |
|
232 | |||
233 | # Create a KernelManager and start a kernel. |
|
233 | # Create a KernelManager and start a kernel. | |
234 |
kernel_manager = KernelManager( |
|
234 | kernel_manager = KernelManager(shell_address=(args.ip, args.shell), | |
235 |
sub_address=(args.ip, args. |
|
235 | sub_address=(args.ip, args.iopub), | |
236 |
|
|
236 | stdin_address=(args.ip, args.stdin), | |
237 | hb_address=(args.ip, args.hb)) |
|
237 | hb_address=(args.ip, args.hb)) | |
238 | if not args.existing: |
|
238 | if not args.existing: | |
239 | # if not args.ip in LOCAL_IPS+ALL_ALIAS: |
|
239 | # if not args.ip in LOCAL_IPS+ALL_ALIAS: | |
240 | # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) |
|
240 | # raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS) | |
241 |
|
241 | |||
242 | kwargs = dict(ip=args.ip) |
|
242 | kwargs = dict(ip=args.ip, ipython=True) | |
243 | if args.pure: |
|
|||
244 | kwargs['ipython']=False |
|
|||
245 | else: |
|
|||
246 | kwargs['colors']=colors |
|
|||
247 | if args.pylab: |
|
|||
248 | kwargs['pylab']=args.pylab |
|
|||
249 | kernel_manager.start_kernel(**kwargs) |
|
243 | kernel_manager.start_kernel(**kwargs) | |
250 |
|
244 | |||
251 |
|
245 | |||
252 | kernel_manager.start_channels() |
|
246 | kernel_manager.start_channels() | |
253 |
|
247 | |||
254 | frontend=Frontend(kernel_manager) |
|
248 | frontend=Frontend(kernel_manager) | |
255 | return frontend |
|
249 | return frontend | |
256 |
|
250 | |||
257 | if __name__ == "__main__" : |
|
251 | if __name__ == "__main__" : | |
258 | frontend=start_frontend() |
|
252 | frontend=start_frontend() | |
259 | frontend.start() |
|
253 | frontend.start() |
@@ -1,152 +1,152 b'' | |||||
1 | """Implement a fully blocking kernel manager. |
|
1 | """Implement a fully blocking kernel manager. | |
2 |
|
2 | |||
3 | Useful for test suites and blocking terminal interfaces. |
|
3 | Useful for test suites and blocking terminal interfaces. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (C) 2010-2011 The IPython Development Team |
|
6 | # Copyright (C) 2010-2011 The IPython Development Team | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | # Distributed under the terms of the BSD License. The full license is in | |
9 | # the file COPYING.txt, distributed as part of this software. |
|
9 | # the file COPYING.txt, distributed as part of this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | from __future__ import print_function |
|
15 | from __future__ import print_function | |
16 |
|
16 | |||
17 | # Stdlib |
|
17 | # Stdlib | |
18 | from Queue import Queue, Empty |
|
18 | from Queue import Queue, Empty | |
19 | from threading import Event |
|
19 | from threading import Event | |
20 |
|
20 | |||
21 | # Our own |
|
21 | # Our own | |
22 | from IPython.utils import io |
|
22 | from IPython.utils import io | |
23 | from IPython.utils.traitlets import Type |
|
23 | from IPython.utils.traitlets import Type | |
24 |
|
24 | |||
25 | from .kernelmanager import (KernelManager, SubSocketChannel, HBSocketChannel, |
|
25 | from .kernelmanager import (KernelManager, SubSocketChannel, HBSocketChannel, | |
26 | ShellSocketChannel, StdInSocketChannel) |
|
26 | ShellSocketChannel, StdInSocketChannel) | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Functions and classes |
|
29 | # Functions and classes | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 | class BlockingSubSocketChannel(SubSocketChannel): |
|
32 | class BlockingSubSocketChannel(SubSocketChannel): | |
33 |
|
33 | |||
34 | def __init__(self, context, session, address=None): |
|
34 | def __init__(self, context, session, address=None): | |
35 | super(BlockingSubSocketChannel, self).__init__(context, session, |
|
35 | super(BlockingSubSocketChannel, self).__init__(context, session, | |
36 | address) |
|
36 | address) | |
37 | self._in_queue = Queue() |
|
37 | self._in_queue = Queue() | |
38 |
|
38 | |||
39 | def call_handlers(self, msg): |
|
39 | def call_handlers(self, msg): | |
40 | #io.rprint('[[Sub]]', msg) # dbg |
|
40 | #io.rprint('[[Sub]]', msg) # dbg | |
41 | self._in_queue.put(msg) |
|
41 | self._in_queue.put(msg) | |
42 |
|
42 | |||
43 | def msg_ready(self): |
|
43 | def msg_ready(self): | |
44 | """Is there a message that has been received?""" |
|
44 | """Is there a message that has been received?""" | |
45 | if self._in_queue.qsize() == 0: |
|
45 | if self._in_queue.qsize() == 0: | |
46 | return False |
|
46 | return False | |
47 | else: |
|
47 | else: | |
48 | return True |
|
48 | return True | |
49 |
|
49 | |||
50 | def get_msg(self, block=True, timeout=None): |
|
50 | def get_msg(self, block=True, timeout=None): | |
51 | """Get a message if there is one that is ready.""" |
|
51 | """Get a message if there is one that is ready.""" | |
52 | if block and timeout is None: |
|
52 | if block and timeout is None: | |
53 | # never use timeout=None, because get |
|
53 | # never use timeout=None, because get | |
54 | # becomes uninterruptible |
|
54 | # becomes uninterruptible | |
55 | timeout = 1e6 |
|
55 | timeout = 1e6 | |
56 | return self._in_queue.get(block, timeout) |
|
56 | return self._in_queue.get(block, timeout) | |
57 |
|
57 | |||
58 | def get_msgs(self): |
|
58 | def get_msgs(self): | |
59 | """Get all messages that are currently ready.""" |
|
59 | """Get all messages that are currently ready.""" | |
60 | msgs = [] |
|
60 | msgs = [] | |
61 | while True: |
|
61 | while True: | |
62 | try: |
|
62 | try: | |
63 | msgs.append(self.get_msg(block=False)) |
|
63 | msgs.append(self.get_msg(block=False)) | |
64 | except Empty: |
|
64 | except Empty: | |
65 | break |
|
65 | break | |
66 | return msgs |
|
66 | return msgs | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | class BlockingShellSocketChannel(ShellSocketChannel): |
|
69 | class BlockingShellSocketChannel(ShellSocketChannel): | |
70 |
|
70 | |||
71 | def __init__(self, context, session, address=None): |
|
71 | def __init__(self, context, session, address=None): | |
72 | super(BlockingShellSocketChannel, self).__init__(context, session, |
|
72 | super(BlockingShellSocketChannel, self).__init__(context, session, | |
73 | address) |
|
73 | address) | |
74 | self._in_queue = Queue() |
|
74 | self._in_queue = Queue() | |
75 |
|
75 | |||
76 | def call_handlers(self, msg): |
|
76 | def call_handlers(self, msg): | |
77 | #io.rprint('[[Shell]]', msg) # dbg |
|
77 | #io.rprint('[[Shell]]', msg) # dbg | |
78 | self._in_queue.put(msg) |
|
78 | self._in_queue.put(msg) | |
79 |
|
79 | |||
80 | def msg_ready(self): |
|
80 | def msg_ready(self): | |
81 | """Is there a message that has been received?""" |
|
81 | """Is there a message that has been received?""" | |
82 | if self._in_queue.qsize() == 0: |
|
82 | if self._in_queue.qsize() == 0: | |
83 | return False |
|
83 | return False | |
84 | else: |
|
84 | else: | |
85 | return True |
|
85 | return True | |
86 |
|
86 | |||
87 | def get_msg(self, block=True, timeout=None): |
|
87 | def get_msg(self, block=True, timeout=None): | |
88 | """Get a message if there is one that is ready.""" |
|
88 | """Get a message if there is one that is ready.""" | |
89 | if block and timeout is None: |
|
89 | if block and timeout is None: | |
90 | # never use timeout=None, because get |
|
90 | # never use timeout=None, because get | |
91 | # becomes uninterruptible |
|
91 | # becomes uninterruptible | |
92 | timeout = 1e6 |
|
92 | timeout = 1e6 | |
93 | return self._in_queue.get(block, timeout) |
|
93 | return self._in_queue.get(block, timeout) | |
94 |
|
94 | |||
95 | def get_msgs(self): |
|
95 | def get_msgs(self): | |
96 | """Get all messages that are currently ready.""" |
|
96 | """Get all messages that are currently ready.""" | |
97 | msgs = [] |
|
97 | msgs = [] | |
98 | while True: |
|
98 | while True: | |
99 | try: |
|
99 | try: | |
100 | msgs.append(self.get_msg(block=False)) |
|
100 | msgs.append(self.get_msg(block=False)) | |
101 | except Empty: |
|
101 | except Empty: | |
102 | break |
|
102 | break | |
103 | return msgs |
|
103 | return msgs | |
104 |
|
104 | |||
105 |
|
105 | |||
106 | class BlockingStdInSocketChannel(StdInSocketChannel): |
|
106 | class BlockingStdInSocketChannel(StdInSocketChannel): | |
107 |
|
107 | |||
108 | def __init__(self, context, session, address=None): |
|
108 | def __init__(self, context, session, address=None): | |
109 |
super(Blocking |
|
109 | super(BlockingStdInSocketChannel, self).__init__(context, session, address) | |
110 | self._in_queue = Queue() |
|
110 | self._in_queue = Queue() | |
111 |
|
111 | |||
112 | def call_handlers(self, msg): |
|
112 | def call_handlers(self, msg): | |
113 | #io.rprint('[[Rep]]', msg) # dbg |
|
113 | #io.rprint('[[Rep]]', msg) # dbg | |
114 | self._in_queue.put(msg) |
|
114 | self._in_queue.put(msg) | |
115 |
|
115 | |||
116 | def get_msg(self, block=True, timeout=None): |
|
116 | def get_msg(self, block=True, timeout=None): | |
117 | "Gets a message if there is one that is ready." |
|
117 | "Gets a message if there is one that is ready." | |
118 | return self._in_queue.get(block, timeout) |
|
118 | return self._in_queue.get(block, timeout) | |
119 |
|
119 | |||
120 | def get_msgs(self): |
|
120 | def get_msgs(self): | |
121 | """Get all messages that are currently ready.""" |
|
121 | """Get all messages that are currently ready.""" | |
122 | msgs = [] |
|
122 | msgs = [] | |
123 | while True: |
|
123 | while True: | |
124 | try: |
|
124 | try: | |
125 | msgs.append(self.get_msg(block=False)) |
|
125 | msgs.append(self.get_msg(block=False)) | |
126 | except Empty: |
|
126 | except Empty: | |
127 | break |
|
127 | break | |
128 | return msgs |
|
128 | return msgs | |
129 |
|
129 | |||
130 | def msg_ready(self): |
|
130 | def msg_ready(self): | |
131 | "Is there a message that has been received?" |
|
131 | "Is there a message that has been received?" | |
132 | return not self._in_queue.empty() |
|
132 | return not self._in_queue.empty() | |
133 |
|
133 | |||
134 |
|
134 | |||
135 | class BlockingHBSocketChannel(HBSocketChannel): |
|
135 | class BlockingHBSocketChannel(HBSocketChannel): | |
136 |
|
136 | |||
137 | # This kernel needs rapid monitoring capabilities |
|
137 | # This kernel needs rapid monitoring capabilities | |
138 | time_to_dead = 0.2 |
|
138 | time_to_dead = 0.2 | |
139 |
|
139 | |||
140 | def call_handlers(self, since_last_heartbeat): |
|
140 | def call_handlers(self, since_last_heartbeat): | |
141 | #io.rprint('[[Heart]]', since_last_heartbeat) # dbg |
|
141 | #io.rprint('[[Heart]]', since_last_heartbeat) # dbg | |
142 | pass |
|
142 | pass | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | class BlockingKernelManager(KernelManager): |
|
145 | class BlockingKernelManager(KernelManager): | |
146 |
|
146 | |||
147 | # The classes to use for the various channels. |
|
147 | # The classes to use for the various channels. | |
148 | shell_channel_class = Type(BlockingShellSocketChannel) |
|
148 | shell_channel_class = Type(BlockingShellSocketChannel) | |
149 | sub_channel_class = Type(BlockingSubSocketChannel) |
|
149 | sub_channel_class = Type(BlockingSubSocketChannel) | |
150 | stdin_channel_class = Type(BlockingStdInSocketChannel) |
|
150 | stdin_channel_class = Type(BlockingStdInSocketChannel) | |
151 | hb_channel_class = Type(BlockingHBSocketChannel) |
|
151 | hb_channel_class = Type(BlockingHBSocketChannel) | |
152 |
|
152 |
General Comments 0
You need to be logged in to leave comments.
Login now