Show More
@@ -74,6 +74,7 b' from IPython.kernel.zmq.session import default_secure, Session' | |||
|
74 | 74 | from IPython.nbformat.sign import NotebookNotary |
|
75 | 75 | from IPython.utils.importstring import import_item |
|
76 | 76 | from IPython.utils import submodule |
|
77 | from IPython.utils.process import check_pid | |
|
77 | 78 | from IPython.utils.traitlets import ( |
|
78 | 79 | Dict, Unicode, Integer, List, Bool, Bytes, Instance, |
|
79 | 80 | DottedObjectName, TraitError, |
@@ -841,6 +842,7 b' class NotebookApp(BaseIPythonApplication):' | |||
|
841 | 842 | 'secure': bool(self.certfile), |
|
842 | 843 | 'base_url': self.base_url, |
|
843 | 844 | 'notebook_dir': os.path.abspath(self.notebook_dir), |
|
845 | 'pid': os.getpid() | |
|
844 | 846 | } |
|
845 | 847 | |
|
846 | 848 | def write_server_info_file(self): |
@@ -914,8 +916,17 b" def list_running_servers(profile='default'):" | |||
|
914 | 916 | for file in os.listdir(pd.security_dir): |
|
915 | 917 | if file.startswith('nbserver-'): |
|
916 | 918 | with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f: |
|
917 |
|
|
|
919 | info = json.load(f) | |
|
918 | 920 | |
|
921 | # Simple check whether that process is really still running | |
|
922 | if check_pid(info['pid']): | |
|
923 | yield info | |
|
924 | else: | |
|
925 | # If the process has died, try to delete its info file | |
|
926 | try: | |
|
927 | os.unlink(file) | |
|
928 | except OSError: | |
|
929 | pass # TODO: This should warn or log or something | |
|
919 | 930 | #----------------------------------------------------------------------------- |
|
920 | 931 | # Main entry point |
|
921 | 932 | #----------------------------------------------------------------------------- |
@@ -36,6 +36,7 b' from IPython.core.application import (' | |||
|
36 | 36 | base_flags as base_ip_flags |
|
37 | 37 | ) |
|
38 | 38 | from IPython.utils.path import expand_path |
|
39 | from IPython.utils.process import check_pid | |
|
39 | 40 | from IPython.utils import py3compat |
|
40 | 41 | from IPython.utils.py3compat import unicode_type |
|
41 | 42 | |
@@ -249,28 +250,11 b' class BaseParallelApplication(BaseIPythonApplication):' | |||
|
249 | 250 | raise PIDFileError('pid file not found: %s' % pid_file) |
|
250 | 251 | |
|
251 | 252 | def check_pid(self, pid): |
|
252 | if os.name == 'nt': | |
|
253 | try: | |
|
254 | import ctypes | |
|
255 | # returns 0 if no such process (of ours) exists | |
|
256 | # positive int otherwise | |
|
257 | p = ctypes.windll.kernel32.OpenProcess(1,0,pid) | |
|
258 | except Exception: | |
|
259 | self.log.warn( | |
|
260 | "Could not determine whether pid %i is running via `OpenProcess`. " | |
|
261 | " Making the likely assumption that it is."%pid | |
|
262 | ) | |
|
263 | return True | |
|
264 | return bool(p) | |
|
265 | else: | |
|
266 | try: | |
|
267 | p = Popen(['ps','x'], stdout=PIPE, stderr=PIPE) | |
|
268 | output,_ = p.communicate() | |
|
269 | except OSError: | |
|
270 | self.log.warn( | |
|
271 | "Could not determine whether pid %i is running via `ps x`. " | |
|
272 | " Making the likely assumption that it is."%pid | |
|
273 | ) | |
|
274 | return True | |
|
275 | pids = list(map(int, re.findall(br'^\W*\d+', output, re.MULTILINE))) | |
|
276 | return pid in pids | |
|
253 | try: | |
|
254 | return check_pid(pid) | |
|
255 | except Exception: | |
|
256 | self.log.warn( | |
|
257 | "Could not determine whether pid %i is running. " | |
|
258 | " Making the likely assumption that it is."%pid | |
|
259 | ) | |
|
260 | return True |
@@ -16,6 +16,8 b' This file is only meant to be imported by process.py, not by end-users.' | |||
|
16 | 16 | from __future__ import print_function |
|
17 | 17 | |
|
18 | 18 | # Stdlib |
|
19 | import errno | |
|
20 | import os | |
|
19 | 21 | import subprocess as sp |
|
20 | 22 | import sys |
|
21 | 23 | |
@@ -209,5 +211,15 b' class ProcessHandler(object):' | |||
|
209 | 211 | # (ls is a good example) that makes them hard. |
|
210 | 212 | system = ProcessHandler().system |
|
211 | 213 | |
|
212 | ||
|
213 | ||
|
214 | def check_pid(pid): | |
|
215 | try: | |
|
216 | os.kill(pid, 0) | |
|
217 | except OSError as err: | |
|
218 | if err.errno == errno.ESRCH: | |
|
219 | return False | |
|
220 | elif err.errno == errno.EPERM: | |
|
221 | # Don't have permission to signal the process - probably means it exists | |
|
222 | return True | |
|
223 | raise | |
|
224 | else: | |
|
225 | return True |
@@ -185,3 +185,8 b' try:' | |||
|
185 | 185 | return result |
|
186 | 186 | except AttributeError: |
|
187 | 187 | arg_split = py_arg_split |
|
188 | ||
|
189 | def check_pid(pid): | |
|
190 | # OpenProcess returns 0 if no such process (of ours) exists | |
|
191 | # positive int otherwise | |
|
192 | return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid)) |
@@ -21,11 +21,11 b' import sys' | |||
|
21 | 21 | |
|
22 | 22 | # Our own |
|
23 | 23 | if sys.platform == 'win32': |
|
24 | from ._process_win32 import _find_cmd, system, getoutput, arg_split | |
|
24 | from ._process_win32 import _find_cmd, system, getoutput, arg_split, check_pid | |
|
25 | 25 | elif sys.platform == 'cli': |
|
26 | 26 | from ._process_cli import _find_cmd, system, getoutput, arg_split |
|
27 | 27 | else: |
|
28 | from ._process_posix import _find_cmd, system, getoutput, arg_split | |
|
28 | from ._process_posix import _find_cmd, system, getoutput, arg_split, check_pid | |
|
29 | 29 | |
|
30 | 30 | from ._process_common import getoutputerror, get_output_error_code, process_handler |
|
31 | 31 | from . import py3compat |
General Comments 0
You need to be logged in to leave comments.
Login now