##// END OF EJS Templates
disable subprocess pipe in inprocess kernel...
MinRK -
Show More
@@ -1,177 +1,177 b''
1 1 """An in-process kernel"""
2 2
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2012 The IPython Development Team
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13
14 14 # Standard library imports
15 15 from contextlib import contextmanager
16 16 import logging
17 17 import sys
18 18
19 19 # Local imports
20 20 from IPython.core.interactiveshell import InteractiveShellABC
21 21 from IPython.utils.jsonutil import json_clean
22 22 from IPython.utils.traitlets import Any, Enum, Instance, List, Type
23 23 from IPython.kernel.zmq.ipkernel import Kernel
24 24 from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell
25 25
26 26 from .socket import DummySocket
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Main kernel class
30 30 #-----------------------------------------------------------------------------
31 31
32 32 class InProcessKernel(Kernel):
33 33
34 34 #-------------------------------------------------------------------------
35 35 # InProcessKernel interface
36 36 #-------------------------------------------------------------------------
37 37
38 38 # The frontends connected to this kernel.
39 39 frontends = List(
40 40 Instance('IPython.kernel.inprocess.kernelmanager.InProcessKernelManager'))
41 41
42 42 # The GUI environment that the kernel is running under. This need not be
43 43 # specified for the normal operation for the kernel, but is required for
44 44 # IPython's GUI support (including pylab). The default is 'inline' because
45 45 # it is safe under all GUI toolkits.
46 46 gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
47 47 default_value='inline')
48 48
49 49 raw_input_str = Any()
50 50 stdout = Any()
51 51 stderr = Any()
52 52
53 53 #-------------------------------------------------------------------------
54 54 # Kernel interface
55 55 #-------------------------------------------------------------------------
56 56
57 57 shell_class = Type()
58 58 shell_streams = List()
59 59 control_stream = Any()
60 60 iopub_socket = Instance(DummySocket, ())
61 61 stdin_socket = Instance(DummySocket, ())
62 62
63 63 def __init__(self, **traits):
64 64 # When an InteractiveShell is instantiated by our base class, it binds
65 65 # the current values of sys.stdout and sys.stderr.
66 66 with self._redirected_io():
67 67 super(InProcessKernel, self).__init__(**traits)
68 68
69 69 self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
70 70 self.shell.kernel = self
71 71
72 72 def execute_request(self, stream, ident, parent):
73 73 """ Override for temporary IO redirection. """
74 74 with self._redirected_io():
75 75 super(InProcessKernel, self).execute_request(stream, ident, parent)
76 76
77 77 def start(self):
78 78 """ Override registration of dispatchers for streams. """
79 79 self.shell.exit_now = False
80 80
81 81 def _abort_queue(self, stream):
82 82 """ The in-process kernel doesn't abort requests. """
83 83 pass
84 84
85 85 def _raw_input(self, prompt, ident, parent):
86 86 # Flush output before making the request.
87 87 self.raw_input_str = None
88 88 sys.stderr.flush()
89 89 sys.stdout.flush()
90 90
91 91 # Send the input request.
92 92 content = json_clean(dict(prompt=prompt))
93 93 msg = self.session.msg(u'input_request', content, parent)
94 94 for frontend in self.frontends:
95 95 if frontend.session.session == parent['header']['session']:
96 96 frontend.stdin_channel.call_handlers(msg)
97 97 break
98 98 else:
99 99 logging.error('No frontend found for raw_input request')
100 100 return str()
101 101
102 102 # Await a response.
103 103 while self.raw_input_str is None:
104 104 frontend.stdin_channel.process_events()
105 105 return self.raw_input_str
106 106
107 107 #-------------------------------------------------------------------------
108 108 # Protected interface
109 109 #-------------------------------------------------------------------------
110 110
111 111 @contextmanager
112 112 def _redirected_io(self):
113 113 """ Temporarily redirect IO to the kernel.
114 114 """
115 115 sys_stdout, sys_stderr = sys.stdout, sys.stderr
116 116 sys.stdout, sys.stderr = self.stdout, self.stderr
117 117 yield
118 118 sys.stdout, sys.stderr = sys_stdout, sys_stderr
119 119
120 120 #------ Trait change handlers --------------------------------------------
121 121
122 122 def _io_dispatch(self):
123 123 """ Called when a message is sent to the IO socket.
124 124 """
125 125 ident, msg = self.session.recv(self.iopub_socket, copy=False)
126 126 for frontend in self.frontends:
127 127 frontend.iopub_channel.call_handlers(msg)
128 128
129 129 #------ Trait initializers -----------------------------------------------
130 130
131 131 def _log_default(self):
132 132 return logging.getLogger(__name__)
133 133
134 134 def _session_default(self):
135 135 from IPython.kernel.zmq.session import Session
136 136 return Session(config=self.config)
137 137
138 138 def _shell_class_default(self):
139 139 return InProcessInteractiveShell
140 140
141 141 def _stdout_default(self):
142 142 from IPython.kernel.zmq.iostream import OutStream
143 return OutStream(self.session, self.iopub_socket, u'stdout')
143 return OutStream(self.session, self.iopub_socket, u'stdout', pipe=False)
144 144
145 145 def _stderr_default(self):
146 146 from IPython.kernel.zmq.iostream import OutStream
147 return OutStream(self.session, self.iopub_socket, u'stderr')
147 return OutStream(self.session, self.iopub_socket, u'stderr', pipe=False)
148 148
149 149 #-----------------------------------------------------------------------------
150 150 # Interactive shell subclass
151 151 #-----------------------------------------------------------------------------
152 152
153 153 class InProcessInteractiveShell(ZMQInteractiveShell):
154 154
155 155 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
156 156
157 157 #-------------------------------------------------------------------------
158 158 # InteractiveShell interface
159 159 #-------------------------------------------------------------------------
160 160
161 161 def enable_gui(self, gui=None):
162 162 """ Enable GUI integration for the kernel.
163 163 """
164 164 from IPython.kernel.zmq.eventloops import enable_gui
165 165 if not gui:
166 166 gui = self.kernel.gui
167 167 enable_gui(gui, kernel=self.kernel)
168 168
169 169 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
170 170 """ Activate pylab support at runtime.
171 171 """
172 172 if not gui:
173 173 gui = self.kernel.gui
174 174 super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
175 175 welcome_message)
176 176
177 177 InteractiveShellABC.register(InProcessInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now