Path: blob/main/tests/complex/traci/connection/multipleConnections/concurringSubscriptions/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 # noqa33import traci.constants as tc # noqa3435PORT = sumolib.miscutils.getFreeSocketPort()36DELTA_T = 100037sumoBinary = sumolib.checkBinary(sys.argv[1])383940def traciLoop(port, traciEndTime, i, runNr, steplength=0):41orderTime = 0.2542time.sleep(orderTime * i) # assure ordering of outputs43if steplength == 0:44steplength = DELTA_T / 1000.45# order index dependent on runNr46index = i if (runNr % 2 == 0) else 10 - i47sys.stdout.flush()48traci.init(port)49traci.setOrder(index)50message = ("Starting process %s (order: %s) with steplength %s\n" % (i, index, steplength))51step = 152vehID = ""53traciEndStep = math.ceil(traciEndTime / steplength)54vehResults = sorted((k, sorted(v.items())) for k, v in traci.vehicle.getAllSubscriptionResults().items())55simResults = traci.simulation.getSubscriptionResults()56while not step > traciEndStep:57message = ""58message += ("Process %s:\n" % (i))59message += (" %s vehicle subscription results: %s\n" % (i, vehResults))60message += (" %s simulation subscription results: %s\n" % (i, simResults))61if (vehID == ""):62vehs = traci.vehicle.getIDList()63if len(vehs) > 0:64vehID = vehs[0]65if i == 1:66message += (" %s subscribing to speed (ID = %s) of vehicle '%s'\n" % (i, tc.VAR_SPEED, vehID))67traci.vehicle.subscribe(vehID, [tc.VAR_SPEED])68message += (" -> %s\n" % str(traci.vehicle.getAllSubscriptionResults()))69else:70message += (" %s subscribing to acceleration (ID = %s) of vehicle '%s'\n" %71(i, tc.VAR_ACCEL, vehID))72traci.vehicle.subscribe(vehID, [tc.VAR_ACCEL])73message += (" -> %s\n" % str(traci.vehicle.getAllSubscriptionResults()))74sys.stdout.flush()75elif len(vehs) == 0:76message += (" %s breaking execution: traced vehicle '%s' left." % (i, vehID))77print(message)78sys.stdout.flush()79break80message += (" %s stepping (step %s)..." % (i, step))81print(message)82sys.stdout.flush()83message = ""84time.sleep(0.01) # give message time to be printed85traci.simulationStep(step * steplength)86simResults = traci.simulation.getSubscriptionResults()87vehResults = sorted((k, sorted(v.items())) for k, v in traci.vehicle.getAllSubscriptionResults().items())88step += 189endTime = traci.simulation.getTime()90traci.close()91time.sleep(orderTime * i) # assure ordering of outputs92print("Process %s (order %s) ended at step %s" % (i, index, endTime))93sys.stdout.flush()949596def runSingle(sumoEndTime, traciEndTime, numClients, runNr):97sumoProcess = subprocess.Popen(98"%s -v --num-clients %s -c sumo.sumocfg -S -Q --remote-port %s" %99(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering100procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, (i + 1), runNr))101for i in range(numClients)]102for p in procs:103p.start()104for p in procs:105p.join()106sumoProcess.wait()107sys.stdout.flush()108109110if __name__ == '__main__':111multiprocessing.set_start_method('spawn')112numClients = 2113runNr = 2114print(" Testing multiclient subscriptions...")115for i in range(0, runNr):116print("\n###### Run %s ######" % i)117sys.stdout.flush()118runSingle(50, 120, numClients, i)119120121