]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
'killSalomeWithPort --spy' fix with psutil vuzlov/20171_psutil
authorViktor Uzlov <viktor.uzlov@opencascade.com>
Fri, 30 Oct 2020 06:21:54 +0000 (09:21 +0300)
committerViktor Uzlov <viktor.uzlov@opencascade.com>
Fri, 30 Oct 2020 06:21:54 +0000 (09:21 +0300)
bin/killSalomeWithPort.py
bin/runSalome.py
bin/salome_utils.py
bin/server.py
src/NamingService/SALOME_NamingServicePy.py

index 9e3875f69d4bcda23a0fcc084a8fc199a5c7003f..f2302dd5f20771b8e4b880a10535cfadbb3e7d3e 100755 (executable)
@@ -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()
index 172298f984413af787eb7b8aa69b5abac9e276ef..93fa749e274e6ad47c3cd391d9a594afcc0ce8bd 100755 (executable)
@@ -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
index f34db2d455dc4a96f3b25accdf373b42e5967263..42383e66909a43486b66b2596d44af254ff292af 100644 (file)
@@ -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
 # --
 
index 855db1949fa7e706b09199cc3a286c7863290747..29c9334f9f714239498563e2b73595cc07e56942 100644 (file)
@@ -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
index 9b5b06db1dc9cba38c0f26208045d5cb016b5fd1..32455869f3079231a690651d0550da5badd0e15e 100644 (file)
@@ -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)
 
     #-------------------------------------------------------------------------