Path: blob/main/tests/complex/traci/connection/multipleConnections/subsecondSUMOstep/runner.py
169771 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, SUMOsteplength, 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 = 149nrEnteredVehicles = 050sumoStop = False51try:52traciEndStep = math.ceil(traciEndTime / steplength)53while not step > traciEndStep:54traci.simulationStep(step * steplength)55# print(index, "asking for vehicles")56# sys.stdout.flush()57traci.vehicle.getIDList()58nrEnteredVehicles += traci.simulation.getDepartedNumber()59# ~ print(index, "Newly entered vehicles: ", traci.simulation.getDepartedNumber(), "(vehs: ", vehs, ")")60# ~ sys.stdout.flush()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" % (index, endTime))75print("Process %s was informed about %s entered vehicles" % (index, nrEnteredVehicles))76sys.stdout.flush()777879def runSingle(sumoEndTime, traciEndTime, numClients, steplengths, runNr, SUMOsteplength):80fdi = open("sumo.sumocfg")81fdo = open("used.sumocfg", "w")82fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": SUMOsteplength})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)88# Alternate ordering89indexRange = range(numClients) if (runNr % 2 == 0) else list(reversed(range(numClients)))90procs = [multiprocessing.Process(target=traciLoop,91args=(PORT, traciEndTime, i + 1, SUMOsteplength, steplengths[indexRange[i]]))92for i in range(numClients)]93for p in procs:94p.start()95for p in procs:96p.join()97sumoProcess.wait()98sys.stdout.flush()99100101if __name__ == '__main__':102multiprocessing.set_start_method('spawn')103runNr = 2104clientRange = [4]105steplengths = [0.1, 1.0, 1.7, 2.0]106SUMOsteplengths = [0.1, 0.5]107print("----------- SUMO ends first -----------")108for numClients in clientRange:109print(" -------- numClients: %s --------" % numClients)110sys.stdout.flush()111for i in range(0, runNr):112print(" Run %s" % i, ", SUMO-steplength=", SUMOsteplengths[i])113sys.stdout.flush()114runSingle(50, 99, numClients, steplengths, i, SUMOsteplengths[i])115116print("----------- TraCI ends first -----------")117for numClients in clientRange:118print(" -------- numClients: %s --------" % numClients)119sys.stdout.flush()120for i in range(0, runNr):121print(" Run %s" % i, ", SUMO-steplength=", SUMOsteplengths[i])122sys.stdout.flush()123runSingle(101, 99, numClients, steplengths, i, SUMOsteplengths[i])124125126