##// END OF EJS Templates
Merge pull request #6099 from takluyver/check-nbservers-pid...
Min RK -
r17279:d01354e9 merge
parent child Browse files
Show More
@@ -74,6 +74,7 b' from IPython.kernel.zmq.session import default_secure, Session'
74 from IPython.nbformat.sign import NotebookNotary
74 from IPython.nbformat.sign import NotebookNotary
75 from IPython.utils.importstring import import_item
75 from IPython.utils.importstring import import_item
76 from IPython.utils import submodule
76 from IPython.utils import submodule
77 from IPython.utils.process import check_pid
77 from IPython.utils.traitlets import (
78 from IPython.utils.traitlets import (
78 Dict, Unicode, Integer, List, Bool, Bytes, Instance,
79 Dict, Unicode, Integer, List, Bool, Bytes, Instance,
79 DottedObjectName, TraitError,
80 DottedObjectName, TraitError,
@@ -841,6 +842,7 b' class NotebookApp(BaseIPythonApplication):'
841 'secure': bool(self.certfile),
842 'secure': bool(self.certfile),
842 'base_url': self.base_url,
843 'base_url': self.base_url,
843 'notebook_dir': os.path.abspath(self.notebook_dir),
844 'notebook_dir': os.path.abspath(self.notebook_dir),
845 'pid': os.getpid()
844 }
846 }
845
847
846 def write_server_info_file(self):
848 def write_server_info_file(self):
@@ -914,8 +916,17 b" def list_running_servers(profile='default'):"
914 for file in os.listdir(pd.security_dir):
916 for file in os.listdir(pd.security_dir):
915 if file.startswith('nbserver-'):
917 if file.startswith('nbserver-'):
916 with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
918 with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
917 yield json.load(f)
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 # Main entry point
931 # Main entry point
921 #-----------------------------------------------------------------------------
932 #-----------------------------------------------------------------------------
@@ -36,6 +36,7 b' from IPython.core.application import ('
36 base_flags as base_ip_flags
36 base_flags as base_ip_flags
37 )
37 )
38 from IPython.utils.path import expand_path
38 from IPython.utils.path import expand_path
39 from IPython.utils.process import check_pid
39 from IPython.utils import py3compat
40 from IPython.utils import py3compat
40 from IPython.utils.py3compat import unicode_type
41 from IPython.utils.py3compat import unicode_type
41
42
@@ -249,28 +250,11 b' class BaseParallelApplication(BaseIPythonApplication):'
249 raise PIDFileError('pid file not found: %s' % pid_file)
250 raise PIDFileError('pid file not found: %s' % pid_file)
250
251
251 def check_pid(self, pid):
252 def check_pid(self, pid):
252 if os.name == 'nt':
253 try:
253 try:
254 return check_pid(pid)
254 import ctypes
255 except Exception:
255 # returns 0 if no such process (of ours) exists
256 self.log.warn(
256 # positive int otherwise
257 "Could not determine whether pid %i is running. "
257 p = ctypes.windll.kernel32.OpenProcess(1,0,pid)
258 " Making the likely assumption that it is."%pid
258 except Exception:
259 )
259 self.log.warn(
260 return True
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
@@ -16,6 +16,8 b' This file is only meant to be imported by process.py, not by end-users.'
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # Stdlib
18 # Stdlib
19 import errno
20 import os
19 import subprocess as sp
21 import subprocess as sp
20 import sys
22 import sys
21
23
@@ -209,5 +211,15 b' class ProcessHandler(object):'
209 # (ls is a good example) that makes them hard.
211 # (ls is a good example) that makes them hard.
210 system = ProcessHandler().system
212 system = ProcessHandler().system
211
213
212
214 def check_pid(pid):
213
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 return result
185 return result
186 except AttributeError:
186 except AttributeError:
187 arg_split = py_arg_split
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 # Our own
22 # Our own
23 if sys.platform == 'win32':
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 elif sys.platform == 'cli':
25 elif sys.platform == 'cli':
26 from ._process_cli import _find_cmd, system, getoutput, arg_split
26 from ._process_cli import _find_cmd, system, getoutput, arg_split
27 else:
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 from ._process_common import getoutputerror, get_output_error_code, process_handler
30 from ._process_common import getoutputerror, get_output_error_code, process_handler
31 from . import py3compat
31 from . import py3compat
General Comments 0
You need to be logged in to leave comments. Login now