diff --git a/src/salome_utils.py b/src/salome_utils.py index f6cbcc2..f2f1fa8 100644 --- a/src/salome_utils.py +++ b/src/salome_utils.py @@ -6,5 +6,46 @@ import salome def hasDesktop() -> bool: return salome.sg.hasDesktop() -def execute(): - pass +def startServer(port, logPath): + + log = open("{}/salome.log".format(logPath), "a") + logging.info("Starting SALOME on port {} ...".format(port)) + + p = subprocess.Popen(["salome", "start", "--port", str(port), "-t"], + shell = True, + stdout = log, + stderr = log) + + log.close() + + return p + +def killServer(port, logPath): + + log = open("{}/salome.log".format(logPath), "a") + logging.info("Terminating SALOME on port {} ...".format(port)) + + p = subprocess.Popen(["salome", "kill", str(port)], + shell = True, + stdout = log, + stderr = log) + + log.close() + + return p + +def execute(port, cmd, logPath): + + log = open("{}/salome.log".format(logPath), "a") + logging.info("Executing command in the SALOME on port {} ...".format(port)) + + p = subprocess.Popen(["salome", "connect", "-p", str(port), str(cmd)], + shell = True, + stdout = log, + stderr = log) + + log.close() + + return p + + diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..fa9d0a4 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,67 @@ +from multiprocessing import Queue, Process, cpu_count + +def queue(cmd, qin, qout, *args): + + while True: + # Get item from the queue + pos, var = qin.get() + + # Exit if last item is None + if pos is None: + break + + # Execute command + res = cmd(var, *args) + + # Put results to the queue + qout.put((pos, res)) + + return + + +def parallel(np, var, cmd): + + varcount = len(var) + + processes = [] + nprocs = np if np <= cpu_count() else cpu_count() + + qin = Queue(1) + qout = Queue() + + # Create processes + for n in range(nprocs): + pargs = [cmd, qin, qout] + + p = Process(target = queue, args = tuple(pargs)) + + processes.append(p) + + # Start processes + for p in processes: + p.daemon = True + p.start() + + # Fill queue + for n in range(varcount): + qin.put((n, var[n])) + + for _ in range(nprocs): + qin.put((None, None)) + + # Get results + results = [[] for n in range(varcount)] + + for n in range(varcount): + index, res = qout.get() + + results[index] = res + + # Wait until each processor has finished + for p in processes: + p.join() + + return results + + +