Path: blob/main/tests/complex/traci/connection/multipleConnections/orderDependentResults/runner.py
169735 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, i, runNr, steplength=0):40orderTime = 0.2541time.sleep(orderTime * i) # assure ordering of outputs42if steplength == 0:43steplength = DELTA_T / 1000.44# order index dependent on runNr45index = i if (runNr % 2 == 0) else 10 - i46print("Starting process %s (order: %s) with steplength %s" % (i, index, steplength))47sys.stdout.flush()48traci.init(port)49traci.setOrder(index)50step = 151lastVehID = ""52traciEndStep = math.ceil(traciEndTime / steplength)53while not step > traciEndStep:54print("Process %s:" % (i))55print(" stepping (step %s)..." % step)56traci.simulationStep(step * steplength)57vehs = traci.vehicle.getIDList()58if len(vehs) != 0:59vehID = vehs[0]60if vehID != lastVehID and lastVehID != "":61print(" breaking execution: traced vehicle '%s' left." % lastVehID)62break63else:64lastVehID = vehID65print(" Retrieving position for vehicle '%s' -> %s on lane '%s'" %66(vehID, traci.vehicle.getLanePosition(vehID), traci.vehicle.getLaneID(vehID)))67print(" Retrieving speed for vehicle '%s' -> %s" % (vehID, traci.vehicle.getSpeed(vehID)))68traci.vehicle.setSpeedMode(vehID, 0)69newSpeed = i * 570print(" Setting speed for vehicle '%s' -> %s" % (vehID, newSpeed))71print(" Retrieving speed for vehicle '%s' -> %s" % (vehID, traci.vehicle.getSpeed(vehID)))72traci.vehicle.setSpeed(vehID, newSpeed)73elif lastVehID != "":74print(" breaking execution: traced vehicle '%s' left." % lastVehID)75break76step += 177sys.stdout.flush()78endTime = traci.simulation.getTime()79traci.close()80time.sleep(orderTime * i) # assure ordering of outputs81print("Process %s (order %s) ended at step %s" % (i, index, endTime))82sys.stdout.flush()838485def runSingle(sumoEndTime, traciEndTime, numClients, runNr):86sumoProcess = subprocess.Popen(87"%s -v --num-clients %s -c sumo.sumocfg -S -Q --remote-port %s" %88(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering89procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, (i + 1), runNr))90for i in range(numClients)]91for p in procs:92p.start()93for p in procs:94p.join()95sumoProcess.wait()96sys.stdout.flush()979899if __name__ == '__main__':100multiprocessing.set_start_method('spawn')101numClients = 2102runNr = 2103print(" Testing client order dependence ...")104for i in range(0, runNr):105print("\n###### Run %s ######" % i)106sys.stdout.flush()107runSingle(50, 500, numClients, i)108109110