From: Viktor UZLOV Date: Tue, 27 Oct 2020 15:39:00 +0000 (+0300) Subject: implementation with psutil X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=0b353acf32aa6d8d7d8b318919c64854e27dd6a6;p=modules%2Fkernel.git implementation with psutil --- diff --git a/bin/killSalome.py b/bin/killSalome.py index 39e51e173..971def6dc 100755 --- a/bin/killSalome.py +++ b/bin/killSalome.py @@ -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': diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index eec6a4465..9e3875f69 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -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)') diff --git a/bin/salome_utils.py b/bin/salome_utils.py index c5d55fcee..f34db2d45 100644 --- a/bin/salome_utils.py +++ b/bin/salome_utils.py @@ -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):