Path: blob/main/tests/complex/traci/connection/multipleConnections/unspecifiedOrderWarning/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 multiprocessing2728if "SUMO_HOME" in os.environ:29sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))30import sumolib # noqa31import traci # noqa3233PORT = sumolib.miscutils.getFreeSocketPort()34DELTA_T = 100035sumoBinary = sumolib.checkBinary(sys.argv[1])363738def traciLoop(port, traciEndTime, index, orderOdd):39orderTime = 0.2540time.sleep(orderTime * index) # assure ordering of outputs41print("Starting process %s" % (index))42sys.stdout.flush()43time.sleep(orderTime * index) # assure ordering of outputs44step = 145try:46traci.init(port)47if orderOdd and index % 2 == 1:48traci.setOrder(index)49sumoStop = False50while not step > traciEndTime:51traci.simulationStep()52vehs = traci.vehicle.getIDList()53if len(vehs) > 3:54print("Something is wrong")55step += 156endTime = traci.simulation.getTime()57traci.close()58# ~ except traci.FatalTraCIError as e:59except Exception as e:60time.sleep(orderTime * index) # assure ordering of outputs61sumoStop = True62print("Process %s: " % index, str(e), " (at TraCIStep %s)" % step)63sys.stdout.flush()64if not sumoStop:65time.sleep(orderTime * index) # assure ordering of outputs66print("Process %s ended at step %s" % (index, endTime))67sys.stdout.flush()686970def runSingle(sumoEndTime, traciEndTime, numClients, orderOdd=False):71fdi = open("sumo.sumocfg")72fdo = open("used.sumocfg", "w")73fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})74fdi.close()75fdo.close()76sumoProcess = subprocess.Popen(77"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %78(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering79procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, i + 1, orderOdd))80for i in range(numClients)]81for p in procs:82p.start()83for p in procs:84p.join()85sumoProcess.wait()86sys.stdout.flush()878889if __name__ == '__main__':90multiprocessing.set_start_method('spawn')91print("----------- Warning Test -----------")92print(" Run 1")93sys.stdout.flush()94runSingle(50, 99, 2)95print(" Run 2")96sys.stdout.flush()97runSingle(50, 99, 2, True)9899100