Path: blob/main/tests/complex/traci/connection/parallelConnection/runner.py
169689 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# @date 2010-02-201718from __future__ import absolute_import19from __future__ import print_function2021import os22import sys2324if "SUMO_HOME" in os.environ:25sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))26import sumolib # noqa27import traci # noqa2829PORT = sumolib.miscutils.getFreeSocketPort()30sumoBinary = sumolib.checkBinary(sys.argv[1])313233def runSingle(sumoEndTime, traciEndTime, label):34fdi = open("sumo.sumocfg")35fdo = open("used.sumocfg", "w")36fdo.write(fdi.read() % {"end": sumoEndTime})37fdi.close()38fdo.close()39step = 040traci.start([sumoBinary, "-c", "used.sumocfg", "-S", "-Q"], port=PORT, label=str(label), stdout=sys.stdout)41while not step > traciEndTime:42traci.simulationStep()43vehs = traci.vehicle.getIDList()44if vehs.index("horiz") < 0 or len(vehs) > 3:45print("Something is wrong")46step += 1474849print("----------- SUMO ends first -----------")50sys.stdout.flush()51for i in range(3):52print(" Run %s" % i)53runSingle(50, 99, i)54for i in range(3):55traci.switch(str(i))56print("Print ended at step %s" % traci.simulation.getTime())57traci.close()58try:59# should throw because everything is closed60traci.simulation.getTime()61except traci.FatalTraCIError as e:62print(e)6364print("----------- TraCI ends first -----------")65sys.stdout.flush()66for i in range(3):67print(" Run %s" % i)68runSingle(101, 99, i)69for i in range(3):70traci.switch(str(i))71print("Print ended at step %s" % traci.simulation.getTime())72traci.close()73try:74# should throw because label isnot known75traci.switch(str(i))76except traci.TraCIException as e:77print(e)787980