##// END OF EJS Templates
split base_launch_kernel into make_kernel_cmd / launch_kernel
MinRK -
Show More
@@ -2,8 +2,7 b''
2 launchers.
2 launchers.
3 """
3 """
4
4
5 # Standard library imports.
5 # Standard library imports
6 import atexit
7 import json
6 import json
8 import os
7 import os
9 import socket
8 import socket
@@ -17,7 +16,7 b' import tempfile'
17 from IPython.utils.localinterfaces import LOCALHOST
16 from IPython.utils.localinterfaces import LOCALHOST
18 from IPython.utils.py3compat import bytes_to_str
17 from IPython.utils.py3compat import bytes_to_str
19
18
20 # Local imports.
19 # Local imports
21 from parentpoller import ParentPollerWindows
20 from parentpoller import ParentPollerWindows
22
21
23 def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
22 def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
@@ -98,9 +97,7 b' def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, '
98 return fname, cfg
97 return fname, cfg
99
98
100
99
101 def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,
100 def make_kernel_cmd(code, executable=None, extra_arguments=[], **kw):
102 executable=None, independent=False, extra_arguments=[],
103 cwd=None):
104 """ Launches a localhost kernel, binding to the specified ports.
101 """ Launches a localhost kernel, binding to the specified ports.
105
102
106 Parameters
103 Parameters
@@ -108,17 +105,54 b' def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,'
108 code : str,
105 code : str,
109 A string of Python code that imports and executes a kernel entry point.
106 A string of Python code that imports and executes a kernel entry point.
110
107
111 stdin, stdout, stderr : optional (default None)
108 executable : str, optional (default sys.executable)
112 Standards streams, as defined in subprocess.Popen.
109 The Python executable to use for the kernel process.
113
110
114 fname : unicode, optional
111 extra_arguments : list, optional
115 The JSON connector file, containing ip/port/hmac key information.
112 A list of extra arguments to pass when executing the launch code.
116
113
117 key : str, optional
114 Returns
118 The Session key used for HMAC authentication.
115 -------
119
116
120 executable : str, optional (default sys.executable)
117 A Popen command list
121 The Python executable to use for the kernel process.
118 """
119
120 # Build the kernel launch command.
121 if executable is None:
122 executable = sys.executable
123 arguments = [ executable, '-c', code, '-f', '{connection_file}' ]
124 arguments.extend(extra_arguments)
125
126 # Spawn a kernel.
127 if sys.platform == 'win32':
128
129 # If the kernel is running on pythonw and stdout/stderr are not been
130 # re-directed, it will crash when more than 4KB of data is written to
131 # stdout or stderr. This is a bug that has been with Python for a very
132 # long time; see http://bugs.python.org/issue706263.
133 # A cleaner solution to this problem would be to pass os.devnull to
134 # Popen directly. Unfortunately, that does not work.
135 if executable.endswith('pythonw.exe'):
136 arguments.append('--no-stdout')
137 arguments.append('--no-stderr')
138
139 return arguments
140
141
142 def launch_kernel(cmd, stdin=None, stdout=None, stderr=None,
143 independent=False,
144 cwd=None, ipython_kernel=True,
145 **kw
146 ):
147 """ Launches a localhost kernel, binding to the specified ports.
148
149 Parameters
150 ----------
151 cmd : Popen list,
152 A string of Python code that imports and executes a kernel entry point.
153
154 stdin, stdout, stderr : optional (default None)
155 Standards streams, as defined in subprocess.Popen.
122
156
123 independent : bool, optional (default False)
157 independent : bool, optional (default False)
124 If set, the kernel process is guaranteed to survive if this process
158 If set, the kernel process is guaranteed to survive if this process
@@ -126,24 +160,18 b' def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,'
126 when this process dies. Note that in this case it is still good practice
160 when this process dies. Note that in this case it is still good practice
127 to kill kernels manually before exiting.
161 to kill kernels manually before exiting.
128
162
129 extra_arguments : list, optional
130 A list of extra arguments to pass when executing the launch code.
131
132 cwd : path, optional
163 cwd : path, optional
133 The working dir of the kernel process (default: cwd of this process).
164 The working dir of the kernel process (default: cwd of this process).
134
165
166 ipython_kernel : bool, optional
167 Whether the kernel is an official IPython one,
168 and should get a bit of special treatment.
169
135 Returns
170 Returns
136 -------
171 -------
137 A tuple of form:
138 (kernel_process, shell_port, iopub_port, stdin_port, hb_port)
139 where kernel_process is a Popen object and the ports are integers.
140 """
141
172
142 # Build the kernel launch command.
173 Popen instance for the kernel subprocess
143 if executable is None:
174 """
144 executable = sys.executable
145 arguments = [ executable, '-c', code, '-f', fname ]
146 arguments.extend(extra_arguments)
147
175
148 # Popen will fail (sometimes with a deadlock) if stdin, stdout, and stderr
176 # Popen will fail (sometimes with a deadlock) if stdin, stdout, and stderr
149 # are invalid. Unfortunately, there is in general no way to detect whether
177 # are invalid. Unfortunately, there is in general no way to detect whether
@@ -167,9 +195,11 b' def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,'
167
195
168 # Spawn a kernel.
196 # Spawn a kernel.
169 if sys.platform == 'win32':
197 if sys.platform == 'win32':
198
170 # Create a Win32 event for interrupting the kernel.
199 # Create a Win32 event for interrupting the kernel.
171 interrupt_event = ParentPollerWindows.create_interrupt_event()
200 interrupt_event = ParentPollerWindows.create_interrupt_event()
172 arguments += [ '--interrupt=%i'%interrupt_event ]
201 if ipython_kernel:
202 cmd += [ '--interrupt=%i' % interrupt_event ]
173
203
174 # If the kernel is running on pythonw and stdout/stderr are not been
204 # If the kernel is running on pythonw and stdout/stderr are not been
175 # re-directed, it will crash when more than 4KB of data is written to
205 # re-directed, it will crash when more than 4KB of data is written to
@@ -177,18 +207,19 b' def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,'
177 # long time; see http://bugs.python.org/issue706263.
207 # long time; see http://bugs.python.org/issue706263.
178 # A cleaner solution to this problem would be to pass os.devnull to
208 # A cleaner solution to this problem would be to pass os.devnull to
179 # Popen directly. Unfortunately, that does not work.
209 # Popen directly. Unfortunately, that does not work.
180 if executable.endswith('pythonw.exe'):
210 if cmd[0].endswith('pythonw.exe'):
181 if stdout is None:
211 if stdout is None:
182 arguments.append('--no-stdout')
212 cmd.append('--no-stdout')
183 if stderr is None:
213 if stderr is None:
184 arguments.append('--no-stderr')
214 cmd.append('--no-stderr')
185
215
186 # Launch the kernel process.
216 # Launch the kernel process.
187 if independent:
217 if independent:
188 proc = Popen(arguments,
218 proc = Popen(cmd,
189 creationflags=512, # CREATE_NEW_PROCESS_GROUP
219 creationflags=512, # CREATE_NEW_PROCESS_GROUP
190 stdin=_stdin, stdout=_stdout, stderr=_stderr)
220 stdin=_stdin, stdout=_stdout, stderr=_stderr)
191 else:
221 else:
222 if ipython_kernel:
192 try:
223 try:
193 from _winapi import DuplicateHandle, GetCurrentProcess, \
224 from _winapi import DuplicateHandle, GetCurrentProcess, \
194 DUPLICATE_SAME_ACCESS
225 DUPLICATE_SAME_ACCESS
@@ -199,18 +230,23 b' def base_launch_kernel(code, fname, stdin=None, stdout=None, stderr=None,'
199 handle = DuplicateHandle(pid, pid, pid, 0,
230 handle = DuplicateHandle(pid, pid, pid, 0,
200 True, # Inheritable by new processes.
231 True, # Inheritable by new processes.
201 DUPLICATE_SAME_ACCESS)
232 DUPLICATE_SAME_ACCESS)
202 proc = Popen(arguments + ['--parent=%i'%int(handle)],
233 cmd +=[ '--parent=%i' % handle ]
203 stdin=_stdin, stdout=_stdout, stderr=_stderr)
234
235
236 proc = Popen(cmd,
237 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)
204
238
205 # Attach the interrupt event to the Popen objet so it can be used later.
239 # Attach the interrupt event to the Popen objet so it can be used later.
206 proc.win32_interrupt_event = interrupt_event
240 proc.win32_interrupt_event = interrupt_event
207
241
208 else:
242 else:
209 if independent:
243 if independent:
210 proc = Popen(arguments, preexec_fn=lambda: os.setsid(),
244 proc = Popen(cmd, preexec_fn=lambda: os.setsid(),
211 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)
245 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)
212 else:
246 else:
213 proc = Popen(arguments + ['--parent=1'],
247 if ipython_kernel:
248 cmd += ['--parent=1']
249 proc = Popen(cmd,
214 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)
250 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd)
215
251
216 # Clean up pipes created to work around Popen bug.
252 # Clean up pipes created to work around Popen bug.
General Comments 0
You need to be logged in to leave comments. Login now