]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
implementation with psutil
authorViktor UZLOV <vuzlov@debian10-01.nnov.opencascade.com>
Tue, 27 Oct 2020 15:39:00 +0000 (18:39 +0300)
committerViktor UZLOV <vuzlov@debian10-01.nnov.opencascade.com>
Tue, 27 Oct 2020 15:39:00 +0000 (18:39 +0300)
bin/killSalome.py
bin/killSalomeWithPort.py
bin/salome_utils.py

index 39e51e173de59d9ad39b832452f5a9a4d9fe2c76..971def6dc121856c87818d2090b9202ca6b3b11f 100755 (executable)
@@ -39,20 +39,18 @@ def killAllPorts():
     """
     user = getUserName()
 
-    def _getPortsFromPiDict(hidden, pattern):
+    for hidden in (True, False):
         fpidict = getPiDict('#####', hidden=hidden)
         dirpidict = os.path.dirname(fpidict)
         fpidict = os.path.basename(fpidict)
         fpidict = fpidict.replace('#####', r'(\d*)')
-        fnamere = re.compile(pattern.format(fpidict))
+        fnamere = re.compile("^{}".format(fpidict))
         with suppress(IOError):
-            for _f in os.listdir(dirpidict):
-                _mo = fnamere.match(_f)
-                if _mo:
-                    killMyPort(_mo.group(1))
+            for f in os.listdir(dirpidict):
+                mo = fnamere.match(f)
+                if mo:
+                    killMyPort(mo.group(1))
 
-    _getPortsFromPiDict(True, "^{}")
-    _getPortsFromPiDict(False, "^{}$")
     # kill other processes
     killProcesses(checkUnkilledProcesses())
     if sys.platform != 'win32':
index eec6a44658a87ed2f89885e746b6867205283598..9e3875f69d4bcda23a0fcc084a8fc199a5c7003f 100755 (executable)
@@ -227,23 +227,23 @@ def shutdownMyPort(port, cleanup=True):
 # a RuntimeError is raised; we explicitly exit this function with code 0 to
 # prevent parent thread from crashing.
 
-def killProcesses(_procs):
+def killProcesses(procs):
     '''
     Terminate and kill all psutil.Process()
     :param _procs: list psutil.Process()
     '''
-    for _p in _procs:
-        _p.terminate()
-    _, alive = psutil.wait_procs(_procs, timeout=5)
-    for _p in alive:
-        _p.kill()
+    for p in procs:
+        p.terminate()
+    _, alive = psutil.wait_procs(procs, timeout=5)
+    for p in alive:
+        p.kill()
 
 def __killMyPort(port, filedict):
-    def _killPiDictProcesses(_process_id):
+    def _killProcesses(_process_id):
         procs = []
-        for _p in _process_id:
+        for p in _process_id:
             try:
-                procs.append(psutil.Process(_p))
+                procs.append(psutil.Process(p))
             except psutil.NoSuchProcess:
                 if verbose(): print("  ------------------ process {} : not found".format(_p))
         killProcesses(procs)
@@ -255,7 +255,7 @@ def __killMyPort(port, filedict):
         with open(filedict, 'rb') as fpid:
             process_ids=pickle.load(fpid)
             for process_id in process_ids:
-                _killPiDictProcesses(process_id)
+                _killProcesses(process_id)
     except:
         print("Cannot find or open SALOME PIDs file for port", port)
         pass
@@ -410,15 +410,15 @@ def checkUnkilledProcesses():
     '''
     from salome_utils import getUserName
     processes = list()
-    def compareNames(_process):
+    def compareNames(process):
         # On OS Windows: psutil.Process().username() get 'usergroup' + 'username'
-        return getUserName() == _process.username()
+        return getUserName() == process.username()
 
-    def _getDictfromOutput(_prlist, _cond=None):
-        for _p in psutil.process_iter(['name', 'username']):
+    def _getDictfromOutput(prlist, cond=None):
+        for p in psutil.process_iter(['name', 'username']):
             with suppress(psutil.AccessDenied):
-                if (compareNames(_p) and re.match(_cond, _p.info['name'])):
-                    _prlist.append(_p)
+                if (compareNames(p) and re.match(cond, p.info['name'])):
+                    prlist.append(p)
 
     _getDictfromOutput(processes, '(SALOME_*)')
     _getDictfromOutput(processes, '(omniNames)')
index c5d55fcee8f1e4f05549f2a9f509b72dbb502662..f34db2d455dc4a96f3b25accdf373b42e5967263 100644 (file)
@@ -30,6 +30,8 @@
 # Exported functions
 #
 
+import psutil
+
 __all__ = [
     'getORBcfgInfo',
     'getHostFromORBcfg',
@@ -549,15 +551,15 @@ def getOmniNamesPid(port):
     """
     Return OmniNames pid by port number.
     """
-    import psutil
-    
     proc_names = {}
     template = (port, "omniNames")
+    pid = None
     for p in psutil.process_iter(['pid', 'name']):
         proc_names[p.info['pid']] = p.info['name']
     for c in psutil.net_connections(kind='inet'):
         if (c.laddr.port, proc_names[c.pid]) == template:
-            return c.pid
+            pid = c.pid
+    return pid
 # --
 
 def killOmniNames(port):