Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/basic/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 time
27
import math
28
import multiprocessing
29
30
if "SUMO_HOME" in os.environ:
31
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
32
import sumolib # noqa
33
import traci # noqa
34
35
PORT = sumolib.miscutils.getFreeSocketPort()
36
DELTA_T = 1000
37
sumoBinary = sumolib.checkBinary(sys.argv[1])
38
39
40
def traciLoop(port, traciEndTime, index, steplength=0):
41
orderTime = 0.25
42
time.sleep(orderTime * index) # assure ordering of outputs
43
if steplength == 0:
44
steplength = DELTA_T / 1000.
45
print("Starting process %s with steplength %s" % (index, steplength))
46
sys.stdout.flush()
47
traci.init(port)
48
traci.setOrder(index)
49
step = 1
50
sumoStop = False
51
try:
52
traciEndStep = math.ceil(traciEndTime / steplength)
53
while not step > traciEndStep:
54
traci.simulationStep(step * steplength)
55
# print(index, "asking for vehicles")
56
# sys.stdout.flush()
57
vehs = traci.vehicle.getIDList()
58
# ~ print(index, "vehs: ", vehs)
59
# ~ sys.stdout.flush()
60
if len(vehs) > 3:
61
print("Something is wrong")
62
step += 1
63
endTime = traci.simulation.getTime()
64
traci.close()
65
except traci.FatalTraCIError as e:
66
if str(e) == "connection closed by SUMO":
67
time.sleep(orderTime * index) # assure ordering of outputs
68
sumoStop = True
69
print("client %s: " % index, str(e), " (at TraCIStep %s)" % step)
70
sys.stdout.flush()
71
else:
72
raise
73
if not sumoStop:
74
time.sleep(orderTime * index) # assure ordering of outputs
75
print("Process %s ended at step %s" %
76
(index, endTime))
77
sys.stdout.flush()
78
79
80
def runSingle(sumoEndTime, traciEndTime, numClients, runNr):
81
fdi = open("sumo.sumocfg")
82
fdo = open("used.sumocfg", "w")
83
fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})
84
fdi.close()
85
fdo.close()
86
sumoProcess = subprocess.Popen(
87
"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %
88
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering
89
indexRange = range(numClients) if (runNr % 2 == 0) else reversed(range(numClients))
90
procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, (i + 1))) for i in indexRange]
91
for p in procs:
92
p.start()
93
for p in procs:
94
p.join()
95
sumoProcess.wait()
96
sys.stdout.flush()
97
98
99
if __name__ == '__main__':
100
multiprocessing.set_start_method('spawn')
101
runNr = 2
102
clientRange = [2, 5]
103
print("----------- SUMO ends first -----------")
104
for numClients in clientRange:
105
print(" -------- numClients: %s --------" % numClients)
106
sys.stdout.flush()
107
for i in range(0, runNr):
108
print(" Run %s" % i)
109
sys.stdout.flush()
110
runSingle(50, 99, numClients, i)
111
112
print("----------- TraCI ends first -----------")
113
for numClients in clientRange:
114
print(" -------- numClients: %s --------" % numClients)
115
sys.stdout.flush()
116
for i in range(0, runNr):
117
print(" Run %s" % i)
118
sys.stdout.flush()
119
runSingle(101, 99, numClients, i)
120
121