Commit 2a855e4f authored by ThibautDemare's avatar ThibautDemare

Add a python script to manage execution of several simulations

from subprocess import run, Popen, PIPE
import multiprocessing
import time
from datetime import datetime
def nbContainers():
result=run("docker ps -aq | wc -l", shell=True, stdout=PIPE).stdout.decode('utf-8')
return int(result)
def backup():
running_containers = run("docker ps --format '{{.Names}}'", shell=True, stdout=PIPE, stderr=PIPE).stdout.decode('utf-8').split("\n")
for c in running_containers:
if c: # if c is not empty (it happens for the last element in p)
Popen("docker checkpoint create --leave-running=true --checkpoint-dir='$(pwd)/"+str(c)"/ContainerBackup/' " + c + " " + "checkpoint_" + c)
def checkForBackupNeedsAndWait():
# Should we backup the simulations ?
timeBetweenTwoBackup = datetime.timedelta(minutes=10) # We have one day between two backups
now =
if now > (lastBackup + timeBetweenTwoBackup):
# Sleep a bit: don't need to be awken all the time
nbCpus = multiprocessing.cpu_count()
lastBackup =
maxNbSim = 4
nbSimStarted = 1
while nbSimStarted <= maxNbSim:
resultPath = "run"+str(nbSimStarted)
run("cp -r ./source ./"+str(resultPath), shell = True)
command = ["docker run --rm --name run"+str(nbSimStarted)+" -v $(pwd)/"+str(resultPath)+"/simulation_configs:/simulation_configs -v $(pwd)/"+str(resultPath)+"/Backup:/Backup -v $(pwd)/"+str(resultPath)+"/Agents:/Agents -v $(pwd)/"+str(resultPath)+"/output:/output -v $(pwd)/"+str(resultPath)+"/CSV:/CSV -v $(pwd)/"+str(resultPath)+"/bd:/bd -v $(pwd)/"+str(resultPath)+"/DALSim:/DALSim gama > out"+str(nbSimStarted)+" 2>&1"]
Popen(command, shell = True)
nbSimStarted += 1
time.sleep(30) # wait 30 sec to make sure the container is really started
# Loop if the current number of running containers is greater or equal than the number of available cpus
while nbCpus <= nbContainers():
while nbContainers() > 0:
# We started all the containers, but we still have to wait until their end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment