Parallezation
This commit is contained in:
parent
69f6b787ea
commit
fbb3849ad6
@ -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
|
||||
|
||||
|
||||
|
67
src/utils.py
Normal file
67
src/utils.py
Normal file
@ -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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user