Show More
@@ -1,3 +1,4 b'' | |||||
|
1 | import logging | |||
1 | import sys |
|
2 | import sys | |
2 | import time |
|
3 | import time | |
3 | from cStringIO import StringIO |
|
4 | from cStringIO import StringIO | |
@@ -15,7 +16,7 b' class OutStream(object):' | |||||
15 |
|
16 | |||
16 | # The time interval between automatic flushes, in seconds. |
|
17 | # The time interval between automatic flushes, in seconds. | |
17 | flush_interval = 0.05 |
|
18 | flush_interval = 0.05 | |
18 |
|
19 | _logger = logging.getLogger() | ||
19 | def __init__(self, session, pub_socket, name): |
|
20 | def __init__(self, session, pub_socket, name): | |
20 | self.session = session |
|
21 | self.session = session | |
21 | self.pub_socket = pub_socket |
|
22 | self.pub_socket = pub_socket | |
@@ -39,7 +40,7 b' class OutStream(object):' | |||||
39 | content = {u'name':self.name, u'data':data} |
|
40 | content = {u'name':self.name, u'data':data} | |
40 | msg = self.session.msg(u'stream', content=content, |
|
41 | msg = self.session.msg(u'stream', content=content, | |
41 | parent=self.parent_header) |
|
42 | parent=self.parent_header) | |
42 |
|
|
43 | self._logger.debug(msg) | |
43 | self.pub_socket.send_json(msg) |
|
44 | self.pub_socket.send_json(msg) | |
44 |
|
45 | |||
45 | self._buffer.close() |
|
46 | self._buffer.close() |
@@ -21,7 +21,7 b' import atexit' | |||||
21 | import sys |
|
21 | import sys | |
22 | import time |
|
22 | import time | |
23 | import traceback |
|
23 | import traceback | |
24 |
|
24 | import logging | ||
25 | # System library imports. |
|
25 | # System library imports. | |
26 | import zmq |
|
26 | import zmq | |
27 |
|
27 | |||
@@ -79,7 +79,10 b' class Kernel(Configurable):' | |||||
79 | # This is a dict of port number that the kernel is listening on. It is set |
|
79 | # This is a dict of port number that the kernel is listening on. It is set | |
80 | # by record_ports and used by connect_request. |
|
80 | # by record_ports and used by connect_request. | |
81 | _recorded_ports = None |
|
81 | _recorded_ports = None | |
82 |
|
82 | |||
|
83 | ||||
|
84 | _logger = logging.getLogger() | |||
|
85 | ||||
83 | def __init__(self, **kwargs): |
|
86 | def __init__(self, **kwargs): | |
84 | super(Kernel, self).__init__(**kwargs) |
|
87 | super(Kernel, self).__init__(**kwargs) | |
85 |
|
88 | |||
@@ -122,21 +125,20 b' class Kernel(Configurable):' | |||||
122 | # easier to trace visually the message chain when debugging. Each |
|
125 | # easier to trace visually the message chain when debugging. Each | |
123 | # handler prints its message at the end. |
|
126 | # handler prints its message at the end. | |
124 | # Eventually we'll move these from stdout to a logger. |
|
127 | # Eventually we'll move these from stdout to a logger. | |
125 |
|
|
128 | self._logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***') | |
126 | io.raw_print(' Content: ', msg['content'], |
|
129 | self._logger.debug(' Content: '+str(msg['content'])+'\n --->\n ') | |
127 | '\n --->\n ', sep='', end='') |
|
|||
128 |
|
130 | |||
129 | # Find and call actual handler for message |
|
131 | # Find and call actual handler for message | |
130 | handler = self.handlers.get(msg['msg_type'], None) |
|
132 | handler = self.handlers.get(msg['msg_type'], None) | |
131 | if handler is None: |
|
133 | if handler is None: | |
132 |
|
|
134 | self._logger.error("UNKNOWN MESSAGE TYPE:" +str(msg)) | |
133 | else: |
|
135 | else: | |
134 | handler(ident, msg) |
|
136 | handler(ident, msg) | |
135 |
|
137 | |||
136 | # Check whether we should exit, in case the incoming message set the |
|
138 | # Check whether we should exit, in case the incoming message set the | |
137 | # exit flag on |
|
139 | # exit flag on | |
138 | if self.shell.exit_now: |
|
140 | if self.shell.exit_now: | |
139 |
|
|
141 | self._logger.debug('\nExiting IPython kernel...') | |
140 | # We do a normal, clean exit, which allows any actions registered |
|
142 | # We do a normal, clean exit, which allows any actions registered | |
141 | # via atexit (such as history saving) to take place. |
|
143 | # via atexit (such as history saving) to take place. | |
142 | sys.exit(0) |
|
144 | sys.exit(0) | |
@@ -186,8 +188,8 b' class Kernel(Configurable):' | |||||
186 | code = content[u'code'] |
|
188 | code = content[u'code'] | |
187 | silent = content[u'silent'] |
|
189 | silent = content[u'silent'] | |
188 | except: |
|
190 | except: | |
189 |
|
|
191 | self._logger.error("Got bad msg: ") | |
190 |
|
|
192 | self._logger.error(str(Message(parent))) | |
191 | return |
|
193 | return | |
192 |
|
194 | |||
193 | shell = self.shell # we'll need this a lot here |
|
195 | shell = self.shell # we'll need this a lot here | |
@@ -265,7 +267,7 b' class Kernel(Configurable):' | |||||
265 |
|
267 | |||
266 | # Send the reply. |
|
268 | # Send the reply. | |
267 | reply_msg = self.session.msg(u'execute_reply', reply_content, parent) |
|
269 | reply_msg = self.session.msg(u'execute_reply', reply_content, parent) | |
268 |
|
|
270 | self._logger.debug(str(reply_msg)) | |
269 |
|
271 | |||
270 | # Flush output before sending the reply. |
|
272 | # Flush output before sending the reply. | |
271 | sys.stdout.flush() |
|
273 | sys.stdout.flush() | |
@@ -295,7 +297,7 b' class Kernel(Configurable):' | |||||
295 | 'status' : 'ok'} |
|
297 | 'status' : 'ok'} | |
296 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', |
|
298 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', | |
297 | matches, parent, ident) |
|
299 | matches, parent, ident) | |
298 |
|
|
300 | self._logger.debug(str(completion_msg)) | |
299 |
|
301 | |||
300 | def object_info_request(self, ident, parent): |
|
302 | def object_info_request(self, ident, parent): | |
301 | object_info = self.shell.object_inspect(parent['content']['oname']) |
|
303 | object_info = self.shell.object_inspect(parent['content']['oname']) | |
@@ -303,7 +305,7 b' class Kernel(Configurable):' | |||||
303 | oinfo = json_clean(object_info) |
|
305 | oinfo = json_clean(object_info) | |
304 | msg = self.session.send(self.reply_socket, 'object_info_reply', |
|
306 | msg = self.session.send(self.reply_socket, 'object_info_reply', | |
305 | oinfo, parent, ident) |
|
307 | oinfo, parent, ident) | |
306 | io.raw_print(msg) |
|
308 | self._logger.debug(msg) | |
307 |
|
309 | |||
308 | def history_request(self, ident, parent): |
|
310 | def history_request(self, ident, parent): | |
309 | output = parent['content']['output'] |
|
311 | output = parent['content']['output'] | |
@@ -313,7 +315,7 b' class Kernel(Configurable):' | |||||
313 | content = {'history' : hist} |
|
315 | content = {'history' : hist} | |
314 | msg = self.session.send(self.reply_socket, 'history_reply', |
|
316 | msg = self.session.send(self.reply_socket, 'history_reply', | |
315 | content, parent, ident) |
|
317 | content, parent, ident) | |
316 | io.raw_print(msg) |
|
318 | self._logger.debug(str(msg)) | |
317 |
|
319 | |||
318 | def connect_request(self, ident, parent): |
|
320 | def connect_request(self, ident, parent): | |
319 | if self._recorded_ports is not None: |
|
321 | if self._recorded_ports is not None: | |
@@ -322,7 +324,7 b' class Kernel(Configurable):' | |||||
322 | content = {} |
|
324 | content = {} | |
323 | msg = self.session.send(self.reply_socket, 'connect_reply', |
|
325 | msg = self.session.send(self.reply_socket, 'connect_reply', | |
324 | content, parent, ident) |
|
326 | content, parent, ident) | |
325 | io.raw_print(msg) |
|
327 | self._logger.debug(msg) | |
326 |
|
328 | |||
327 | def shutdown_request(self, ident, parent): |
|
329 | def shutdown_request(self, ident, parent): | |
328 | self.shell.exit_now = True |
|
330 | self.shell.exit_now = True | |
@@ -344,11 +346,11 b' class Kernel(Configurable):' | |||||
344 | assert self.reply_socket.rcvmore(), \ |
|
346 | assert self.reply_socket.rcvmore(), \ | |
345 | "Unexpected missing message part." |
|
347 | "Unexpected missing message part." | |
346 | msg = self.reply_socket.recv_json() |
|
348 | msg = self.reply_socket.recv_json() | |
347 |
|
|
349 | self._logger.debug("Aborting:\n"+str(Message(msg))) | |
348 | msg_type = msg['msg_type'] |
|
350 | msg_type = msg['msg_type'] | |
349 | reply_type = msg_type.split('_')[0] + '_reply' |
|
351 | reply_type = msg_type.split('_')[0] + '_reply' | |
350 | reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) |
|
352 | reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) | |
351 |
|
|
353 | self._logger.debug(reply_msg) | |
352 | self.reply_socket.send(ident,zmq.SNDMORE) |
|
354 | self.reply_socket.send(ident,zmq.SNDMORE) | |
353 | self.reply_socket.send_json(reply_msg) |
|
355 | self.reply_socket.send_json(reply_msg) | |
354 | # We need to wait a bit for requests to come in. This can probably |
|
356 | # We need to wait a bit for requests to come in. This can probably | |
@@ -370,8 +372,8 b' class Kernel(Configurable):' | |||||
370 | try: |
|
372 | try: | |
371 | value = reply['content']['value'] |
|
373 | value = reply['content']['value'] | |
372 | except: |
|
374 | except: | |
373 |
|
|
375 | self._logger.error("Got bad raw_input reply: ") | |
374 |
|
|
376 | self._logger.error(str(Message(parent))) | |
375 | value = '' |
|
377 | value = '' | |
376 | return value |
|
378 | return value | |
377 |
|
379 | |||
@@ -425,7 +427,7 b' class Kernel(Configurable):' | |||||
425 | if self._shutdown_message is not None: |
|
427 | if self._shutdown_message is not None: | |
426 | self.reply_socket.send_json(self._shutdown_message) |
|
428 | self.reply_socket.send_json(self._shutdown_message) | |
427 | self.pub_socket.send_json(self._shutdown_message) |
|
429 | self.pub_socket.send_json(self._shutdown_message) | |
428 |
|
|
430 | self._logger.debug(str(self._shutdown_message)) | |
429 | # A very short sleep to give zmq time to flush its message buffers |
|
431 | # A very short sleep to give zmq time to flush its message buffers | |
430 | # before Python truly shuts down. |
|
432 | # before Python truly shuts down. | |
431 | time.sleep(0.01) |
|
433 | time.sleep(0.01) |
General Comments 0
You need to be logged in to leave comments.
Login now