From 70388a9e6462983a4e95586cdf490b9b03272d78 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Wed, 22 Mar 2017 11:47:26 +0100 Subject: [PATCH] [PortManager] Simplify previous fix; no need to change configuration file contents --- bin/PortManager.py | 33 +++++++++++++-------------------- bin/salomeContext.py | 30 ++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/bin/PortManager.py b/bin/PortManager.py index dfae43a80..18350254d 100644 --- a/bin/PortManager.py +++ b/bin/PortManager.py @@ -147,8 +147,7 @@ def getPort(preferedPort=None): logger.debug("load busy_ports: %s"%str(config["busy_ports"])) # append port - ports_info = config["busy_ports"] - busy_ports = [x[0] for x in ports_info] + busy_ports = config["busy_ports"] port = preferedPort if not port or __isPortUsed(port, busy_ports): port = __PORT_MIN_NUMBER @@ -159,16 +158,11 @@ def getPort(preferedPort=None): msg += "Try to kill the running servers and then launch SALOME again.\n" raise RuntimeError, msg logger.debug("Port %s seems to be busy"%str(port)) - if not port in busy_ports: - config["busy_ports"].append((port,"other")) port = port + 1 logger.debug("found free port: %s"%str(port)) - config["busy_ports"].append((port,"this")) + config["busy_ports"].append(port) # write config, for this application only (i.e. no 'other' ports) - logger.debug("all busy_ports: %s"%str(config["busy_ports"])) - ports_info = config["busy_ports"] - config["busy_ports"] = [x for x in ports_info if x[1] == "this"] logger.debug("write busy_ports: %s"%str(config["busy_ports"])) try: with open(config_file, 'w') as f: @@ -208,12 +202,9 @@ def releasePort(port): # remove port from list ports_info = config["busy_ports"] - config["busy_ports"] = [x for x in ports_info if x[0] != port] + config["busy_ports"] = [x for x in ports_info if x != port] # write config, for this application only (i.e. no 'other' ports) - logger.debug("all busy_ports: %s"%str(config["busy_ports"])) - ports_info = config["busy_ports"] - config["busy_ports"] = [x for x in ports_info if x[1] == "this"] logger.debug("write busy_ports: %s"%str(config["busy_ports"])) try: with open(config_file, 'w') as f: @@ -248,22 +239,24 @@ def getBusyPorts(): logger.debug("load busy_ports: %s"%str(config["busy_ports"])) # Scan all possible ports to determine which ones are owned by other applications - ports_info = config["busy_ports"] - busy_ports = [x[0] for x in ports_info] + ports_info = { 'this': [], 'other': [] } + busy_ports = config["busy_ports"] for port in range(__PORT_MIN_NUMBER, __PORT_MAX_NUMBER): if __isPortUsed(port, busy_ports): logger.debug("Port %s seems to be busy"%str(port)) - if not port in busy_ports: - config["busy_ports"].append((port,"other")) + if port in busy_ports: + ports_info["this"].append(port) + else: + ports_info["other"].append(port) - logger.debug("all busy_ports: %s"%str(config["busy_ports"])) + logger.debug("all busy_ports: %s"%str(ports_info)) - ports_info = config["busy_ports"] - ports_info.sort(key=lambda x: x[0]) + sorted_ports = { 'this': sorted(ports_info['this']), + 'other': sorted(ports_info['other']) } # release lock __release_lock(lock) os.umask(oldmask) - return ports_info + return sorted_ports # diff --git a/bin/salomeContext.py b/bin/salomeContext.py index 86aa86a8f..2b7ffad5d 100644 --- a/bin/salomeContext.py +++ b/bin/salomeContext.py @@ -391,16 +391,15 @@ class SalomeContext: import PortManager # mandatory from multiprocessing import Process from killSalomeWithPort import killMyPort - ports = PortManager.getBusyPorts() + ports = PortManager.getBusyPorts()['this'] if ports: import tempfile - for port,owner in ports: - if owner == "this": - with tempfile.NamedTemporaryFile(): - p = Process(target = killMyPort, args=(port,)) - p.start() - p.join() + for port in ports: + with tempfile.NamedTemporaryFile(): + p = Process(target = killMyPort, args=(port,)) + p.start() + p.join() except ImportError: # :TODO: should be declared obsolete from killSalome import killAllPorts @@ -472,9 +471,20 @@ Available options are: if "-p" in args or "--ports" in args: import PortManager ports = PortManager.getBusyPorts() - print "SALOME instances are running on ports:", ports - if ports: - print "Last started instance on port %s"%ports[-1][0] + this_ports = ports['this'] + other_ports = ports['other'] + if this_ports or other_ports: + print "SALOME instances are running on the following ports:" + if this_ports: + print " This application:", this_ports + else: + print " No SALOME instances of this application" + if other_ports: + print " Other applications:", other_ports + else: + print " No SALOME instances of other applications" + else: + print "No SALOME instances are running" if "-s" in args or "--softwares" in args: if "-s" in args: -- 2.39.2