Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/asyncDisconnect/runner.py
169717 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file runner.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @author Leonhard Luecken
18
# @date 2010-02-20
19
20
from __future__ import absolute_import
21
from __future__ import print_function
22
23
import os
24
import subprocess
25
import sys
26
import math
27
import multiprocessing
28
29
if "SUMO_HOME" in os.environ:
30
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
31
import sumolib # noqa
32
import traci # noqa
33
34
PORT = sumolib.miscutils.getFreeSocketPort()
35
DELTA_T = 1000
36
sumoBinary = sumolib.checkBinary(sys.argv[1])
37
38
39
def traciLoop(port, traciEndTime, index, steplength=0):
40
if steplength == 0:
41
steplength = DELTA_T / 1000.
42
print("Starting process %s with steplength %s" % (index, steplength))
43
sys.stdout.flush()
44
traci.init(port)
45
traci.setOrder(index)
46
step = 1
47
nrEnteredVehicles = 0
48
sumoStop = False
49
try:
50
traciEndStep = math.ceil(traciEndTime / steplength)
51
while not step > traciEndStep:
52
traci.simulationStep(step * steplength)
53
# print(index, "asking for vehicles")
54
# sys.stdout.flush()
55
traci.vehicle.getIDList()
56
nrEnteredVehicles += traci.simulation.getDepartedNumber()
57
# ~ print(index, "Newly entered vehicles: ", traci.simulation.getDepartedNumber(), "(vehs: ", vehs, ")")
58
# ~ sys.stdout.flush()
59
step += 1
60
endTime = traci.simulation.getTime()
61
traci.close()
62
except traci.FatalTraCIError as e:
63
if str(e) == "connection closed by SUMO":
64
sumoStop = True
65
print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)
66
sys.stdout.flush()
67
else:
68
raise
69
if not sumoStop:
70
print("Process %s ended at step %s" % (index, endTime))
71
print("Process %s was informed about %s entered vehicles" % (index, nrEnteredVehicles))
72
sys.stdout.flush()
73
74
75
def runSingle(sumoEndTime, traciEndTime, numClients, steplengths, runNr):
76
fdi = open("sumo.sumocfg")
77
fdo = open("used.sumocfg", "w")
78
fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})
79
fdi.close()
80
fdo.close()
81
sumoProcess = subprocess.Popen(
82
"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %
83
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout)
84
# Alternate ordering
85
indexRange = range(numClients) if (runNr % 2 == 0) else list(reversed(range(numClients)))
86
procs = [multiprocessing.Process(target=traciLoop, args=(
87
PORT, (traciEndTime if i != 0 else 20), i + 1, steplengths[indexRange[i]])) for i in range(numClients)]
88
for p in procs:
89
p.start()
90
for p in procs:
91
p.join()
92
sumoProcess.wait()
93
sys.stdout.flush()
94
95
96
if __name__ == '__main__':
97
multiprocessing.set_start_method('spawn')
98
runNr = 2
99
clientRange = [2, 3]
100
steplengths = [0.1, 1.0, 1.7, 2.0]
101
print("----------- SUMO ends first -----------")
102
for numClients in clientRange:
103
print(" -------- numClients: %s --------" % numClients)
104
sys.stdout.flush()
105
for i in range(0, runNr):
106
print(" Run %s" % i)
107
sys.stdout.flush()
108
runSingle(50, 99, numClients, steplengths, i)
109
110
print("----------- TraCI ends first -----------")
111
for numClients in clientRange:
112
print(" -------- numClients: %s --------" % numClients)
113
sys.stdout.flush()
114
for i in range(0, runNr):
115
print(" Run %s" % i)
116
sys.stdout.flush()
117
runSingle(101, 99, numClients, steplengths, i)
118
119