Path: blob/main/tests/complex/traci/connection/multipleConnections/basic/runner.py
169717 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @author Leonhard Luecken17# @date 2010-02-201819from __future__ import absolute_import20from __future__ import print_function2122import os23import subprocess24import sys25import time26import math27import multiprocessing2829if "SUMO_HOME" in os.environ:30sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))31import sumolib # noqa32import traci # noqa3334PORT = sumolib.miscutils.getFreeSocketPort()35DELTA_T = 100036sumoBinary = sumolib.checkBinary(sys.argv[1])373839def traciLoop(port, traciEndTime, index, steplength=0):40orderTime = 0.2541time.sleep(orderTime * index) # assure ordering of outputs42if steplength == 0:43steplength = DELTA_T / 1000.44print("Starting process %s with steplength %s" % (index, steplength))45sys.stdout.flush()46traci.init(port)47traci.setOrder(index)48step = 149sumoStop = False50try:51traciEndStep = math.ceil(traciEndTime / steplength)52while not step > traciEndStep:53traci.simulationStep(step * steplength)54# print(index, "asking for vehicles")55# sys.stdout.flush()56vehs = traci.vehicle.getIDList()57# ~ print(index, "vehs: ", vehs)58# ~ sys.stdout.flush()59if len(vehs) > 3:60print("Something is wrong")61step += 162endTime = traci.simulation.getTime()63traci.close()64except traci.FatalTraCIError as e:65if str(e) == "connection closed by SUMO":66time.sleep(orderTime * index) # assure ordering of outputs67sumoStop = True68print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)69sys.stdout.flush()70else:71raise72if not sumoStop:73time.sleep(orderTime * index) # assure ordering of outputs74print("Process %s ended at step %s" %75(index, endTime))76sys.stdout.flush()777879def runSingle(sumoEndTime, traciEndTime, numClients, runNr):80fdi = open("sumo.sumocfg")81fdo = open("used.sumocfg", "w")82fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})83fdi.close()84fdo.close()85sumoProcess = subprocess.Popen(86"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %87(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering88indexRange = range(numClients) if (runNr % 2 == 0) else reversed(range(numClients))89procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, (i + 1))) for i in indexRange]90for p in procs:91p.start()92for p in procs:93p.join()94sumoProcess.wait()95sys.stdout.flush()969798if __name__ == '__main__':99multiprocessing.set_start_method('spawn')100runNr = 2101clientRange = [2, 5]102print("----------- SUMO ends first -----------")103for numClients in clientRange:104print(" -------- numClients: %s --------" % numClients)105sys.stdout.flush()106for i in range(0, runNr):107print(" Run %s" % i)108sys.stdout.flush()109runSingle(50, 99, numClients, i)110111print("----------- TraCI ends first -----------")112for numClients in clientRange:113print(" -------- numClients: %s --------" % numClients)114sys.stdout.flush()115for i in range(0, runNr):116print(" Run %s" % i)117sys.stdout.flush()118runSingle(101, 99, numClients, i)119120121