##// END OF EJS Templates
BUG: Ensure that kernel can be spawned from a backgrounded process.
epatters -
Show More
@@ -85,29 +85,36 b' def base_launch_kernel(code, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0'
85 executable = sys.executable
85 executable = sys.executable
86 arguments = [ executable, '-c', code, '--shell=%i'%shell_port,
86 arguments = [ executable, '-c', code, '--shell=%i'%shell_port,
87 '--iopub=%i'%iopub_port, '--stdin=%i'%stdin_port,
87 '--iopub=%i'%iopub_port, '--stdin=%i'%stdin_port,
88 '--hb=%i'%hb_port
88 '--hb=%i'%hb_port ]
89 ]
90 if ip is not None:
89 if ip is not None:
91 arguments.append('--ip=%s'%ip)
90 arguments.append('--ip=%s'%ip)
92 arguments.extend(extra_arguments)
91 arguments.extend(extra_arguments)
93
92
94 # Spawn a kernel.
93 # Popen will fail (sometimes with a deadlock) if stdin, stdout, and stderr
95 if sys.platform == 'win32':
94 # are invalid. Unfortunately, there is in general no way to detect whether
96 # Create a Win32 event for interrupting the kernel.
95 # they are valid. The following two blocks redirect them to (temporary)
97 interrupt_event = ParentPollerWindows.create_interrupt_event()
96 # pipes in certain important cases.
98 arguments += [ '--interrupt=%i'%interrupt_event ]
99
97
100 # If this process in running on pythonw, stdin, stdout, and stderr are
98 # If this process has been backgrounded, our stdin is invalid. Since there
101 # invalid. Popen will fail unless they are suitably redirected. We don't
99 # is no compelling reason for the kernel to inherit our stdin anyway, we'll
102 # read from the pipes, but they must exist.
100 # place this one safe and always redirect.
103 if sys.executable.endswith('pythonw.exe'):
101 redirect_in = True
104 redirect = True
105 _stdin = PIPE if stdin is None else stdin
102 _stdin = PIPE if stdin is None else stdin
103
104 # If this process in running on pythonw, we know that stdin, stdout, and
105 # stderr are all invalid.
106 redirect_out = sys.executable.endswith('pythonw.exe')
107 if redirect_out:
106 _stdout = PIPE if stdout is None else stdout
108 _stdout = PIPE if stdout is None else stdout
107 _stderr = PIPE if stderr is None else stderr
109 _stderr = PIPE if stderr is None else stderr
108 else:
110 else:
109 redirect = False
111 _stdout, _stderr = stdout, stderr
110 _stdin, _stdout, _stderr = stdin, stdout, stderr
112
113 # Spawn a kernel.
114 if sys.platform == 'win32':
115 # Create a Win32 event for interrupting the kernel.
116 interrupt_event = ParentPollerWindows.create_interrupt_event()
117 arguments += [ '--interrupt=%i'%interrupt_event ]
111
118
112 # If the kernel is running on pythonw and stdout/stderr are not been
119 # If the kernel is running on pythonw and stdout/stderr are not been
113 # re-directed, it will crash when more than 4KB of data is written to
120 # re-directed, it will crash when more than 4KB of data is written to
@@ -139,20 +146,22 b' def base_launch_kernel(code, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0'
139 # Attach the interrupt event to the Popen objet so it can be used later.
146 # Attach the interrupt event to the Popen objet so it can be used later.
140 proc.win32_interrupt_event = interrupt_event
147 proc.win32_interrupt_event = interrupt_event
141
148
149 else:
150 if independent:
151 proc = Popen(arguments, preexec_fn=lambda: os.setsid(),
152 stdin=_stdin, stdout=_stdout, stderr=_stderr)
153 else:
154 proc = Popen(arguments + ['--parent=1'],
155 stdin=_stdin, stdout=_stdout, stderr=_stderr)
156
142 # Clean up pipes created to work around Popen bug.
157 # Clean up pipes created to work around Popen bug.
143 if redirect:
158 if redirect_in:
144 if stdin is None:
159 if stdin is None:
145 proc.stdin.close()
160 proc.stdin.close()
161 if redirect_out:
146 if stdout is None:
162 if stdout is None:
147 proc.stdout.close()
163 proc.stdout.close()
148 if stderr is None:
164 if stderr is None:
149 proc.stderr.close()
165 proc.stderr.close()
150
166
151 else:
152 if independent:
153 proc = Popen(arguments, preexec_fn=lambda: os.setsid(),
154 stdin=stdin, stdout=stdout, stderr=stderr)
155 else:
156 proc = Popen(arguments + ['--parent=1'],
157 stdin=stdin, stdout=stdout, stderr=stderr)
158 return proc, shell_port, iopub_port, stdin_port, hb_port
167 return proc, shell_port, iopub_port, stdin_port, hb_port
General Comments 0
You need to be logged in to leave comments. Login now