Path: blob/main/tests/complex/traci/connection/multipleConnections/differentSteplengths/runner.py
169727 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 multiprocessing28from multiprocessing import Process2930if "SUMO_HOME" in os.environ:31sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))32import sumolib # noqa33import traci # noqa3435PORT = sumolib.miscutils.getFreeSocketPort()36DELTA_T = 100037sumoBinary = sumolib.checkBinary(sys.argv[1])383940def traciLoop(port, traciEndTime, index, steplength=0):41orderTime = 0.2542time.sleep(orderTime * index) # assure ordering of outputs43if steplength == 0:44steplength = DELTA_T / 1000.45print("Starting process %s with steplength %s" % (index, steplength))46sys.stdout.flush()47traci.init(port)48traci.setOrder(index)49step = 150nrEnteredVehicles = 051sumoStop = False52try:53traciEndStep = math.ceil(traciEndTime / steplength)54while not step > traciEndStep:55traci.simulationStep(step * steplength)56# print(index, "asking for vehicles")57# sys.stdout.flush()58traci.vehicle.getIDList()59nrEnteredVehicles += traci.simulation.getDepartedNumber()60# ~ print(index, "Newly entered vehicles: ", traci.simulation.getDepartedNumber(), "(vehs: ", vehs, ")")61# ~ sys.stdout.flush()62step += 163endTime = traci.simulation.getTime()64traci.close()65except traci.FatalTraCIError as e:66if str(e) == "connection closed by SUMO":67time.sleep(orderTime * index) # assure ordering of outputs68sumoStop = True69print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)70sys.stdout.flush()71else:72raise73if not sumoStop:74time.sleep(orderTime * index) # assure ordering of outputs75print("Process %s ended at step %s" % (index, endTime))76print("Process %s was informed about %s entered vehicles" % (index, nrEnteredVehicles))77sys.stdout.flush()787980def runSingle(sumoEndTime, traciEndTime, numClients, steplengths, runNr):81fdi = open("sumo.sumocfg")82fdo = open("used.sumocfg", "w")83fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})84fdi.close()85fdo.close()86sumoProcess = subprocess.Popen(87"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %88(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout)89# Alternate ordering90indexRange = range(numClients) if (runNr % 2 == 0) else list(reversed(range(numClients)))91procs = [Process(target=traciLoop, args=(PORT, traciEndTime, i + 1, 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]106print("----------- SUMO ends first -----------")107for numClients in clientRange:108print(" -------- numClients: %s --------" % numClients)109sys.stdout.flush()110for i in range(0, runNr):111print(" Run %s" % i)112sys.stdout.flush()113runSingle(50, 99, numClients, steplengths, i)114115print("----------- TraCI ends first -----------")116for numClients in clientRange:117print(" -------- numClients: %s --------" % numClients)118sys.stdout.flush()119for i in range(0, runNr):120print(" Run %s" % i)121sys.stdout.flush()122runSingle(101, 99, numClients, steplengths, i)123124125