From: Viktor Uzlov Date: Fri, 30 Oct 2020 06:21:54 +0000 (+0300) Subject: 'killSalomeWithPort --spy' fix with psutil X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fvuzlov%2F20171_psutil;p=modules%2Fkernel.git 'killSalomeWithPort --spy' fix with psutil --- diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index 9e3875f69..f2302dd5f 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -384,11 +384,12 @@ def killMyPortSpy(pid, port): import omniORB orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID) import SALOME_NamingServicePy - ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb) + ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb, 3, True) import SALOME #@UnresolvedImport @UnusedImport session = ns.Resolve("/Kernel/Session") assert session except: + killMyPort(port) return try: status = session.GetStatSession() diff --git a/bin/runSalome.py b/bin/runSalome.py index 172298f98..93fa749e2 100755 --- a/bin/runSalome.py +++ b/bin/runSalome.py @@ -899,7 +899,7 @@ def foreGround(clt, args): server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)] else: server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)] - server.run() + server.run(True) # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port)) # -- dt = 1.0 @@ -944,6 +944,8 @@ def runSalome(): test = test and args['foreground'] # -- if test: + from time import sleep + sleep(3.0) foreGround(clt, args) pass pass diff --git a/bin/salome_utils.py b/bin/salome_utils.py index f34db2d45..42383e669 100644 --- a/bin/salome_utils.py +++ b/bin/salome_utils.py @@ -508,42 +508,24 @@ def killpid(pid, sig = 9): -1 Fail, another reason """ - if not pid: return - import os, sys - if sig != 0: + if not pid: + return + + if sig == 0: + ret = 1 if psutil.pid_exists(int(pid)) else 0 + else: if verbose(): print("######## killpid pid = ", pid) - try: - if sys.platform == "win32": - import ctypes - if sig == 0: - # PROCESS_QUERY_INFORMATION (0x0400) Required to retrieve certain information about a process - handle = ctypes.windll.kernel32.OpenProcess(0x0400, False, int(pid)) - if handle: - ret = 1 - ctypes.windll.kernel32.CloseHandle(handle) - else: - ret = 0 - if sig == 9: - # PROCESS_TERMINATE (0x0001) Required to terminate a process using TerminateProcess. - handle = ctypes.windll.kernel32.OpenProcess(0x0001, False, int(pid)) - ret = ctypes.windll.kernel32.TerminateProcess(handle, -1) - ctypes.windll.kernel32.CloseHandle(handle) - pass - pass - else: - # Default: signal.SIGKILL = 9 - os.kill(int(pid),sig) + try: + proc = psutil.Process(int(pid)) + proc.terminate() + _, alive = psutil.wait_procs([proc], timeout=5) + for p in alive: + p.kill() ret = 1 - pass - pass - except OSError as e: - # errno.ESRCH == 3 is 'No such process' - if e.errno == 3: + except psutil.NoSuchProcess: ret = 0 - else: + except OSError: ret = -1 - pass - pass return ret # -- diff --git a/bin/server.py b/bin/server.py index 855db1949..29c9334f9 100644 --- a/bin/server.py +++ b/bin/server.py @@ -56,7 +56,7 @@ class Server: else: raise Exception("Unsupported server launch mode: %s" % mode) - def run(self): + def run(self, daemon=False): global process_id myargs=self.ARGS if self.args.get('xterm'): @@ -73,7 +73,11 @@ class Server: # print("command = ", command) if sys.platform == "win32": import subprocess - pid = subprocess.Popen(command).pid + if daemon: + DETACHED_PROCESS = 0x00000008 + pid = subprocess.Popen(command, creationflags=DETACHED_PROCESS).pid + else: + pid = subprocess.Popen(command).pid elif Server.server_launch_mode == "fork": pid = os.spawnvp(os.P_NOWAIT, command[0], command) else: # Server launch mode is daemon diff --git a/src/NamingService/SALOME_NamingServicePy.py b/src/NamingService/SALOME_NamingServicePy.py index 9b5b06db1..32455869f 100644 --- a/src/NamingService/SALOME_NamingServicePy.py +++ b/src/NamingService/SALOME_NamingServicePy.py @@ -51,7 +51,7 @@ class SALOME_NamingServicePy_i(object): #------------------------------------------------------------------------- - def __init__(self, orb=None): + def __init__(self, orb=None, steps=240, spy=False): """ Standard Constructor, with ORB reference. @@ -63,7 +63,6 @@ class SALOME_NamingServicePy_i(object): self._orb = orb # initialize root context and current context ok = 0 - steps = 240 while steps > 0 and ok == 0: try: obj =self._orb.resolve_initial_references("NameService") @@ -83,7 +82,10 @@ class SALOME_NamingServicePy_i(object): steps = steps - 1 if steps == 0 and self._root_context is None: MESSAGE ( "Name Service Reference is invalid" ) - sys.exit(1) + if spy: + raise ValueError("Name Service Reference is invalid") + else: + sys.exit(1) #-------------------------------------------------------------------------