Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/parallelConnection/runner.py
169689 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
# @date 2010-02-20
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import os
23
import sys
24
25
if "SUMO_HOME" in os.environ:
26
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
27
import sumolib # noqa
28
import traci # noqa
29
30
PORT = sumolib.miscutils.getFreeSocketPort()
31
sumoBinary = sumolib.checkBinary(sys.argv[1])
32
33
34
def runSingle(sumoEndTime, traciEndTime, label):
35
fdi = open("sumo.sumocfg")
36
fdo = open("used.sumocfg", "w")
37
fdo.write(fdi.read() % {"end": sumoEndTime})
38
fdi.close()
39
fdo.close()
40
step = 0
41
traci.start([sumoBinary, "-c", "used.sumocfg", "-S", "-Q"], port=PORT, label=str(label), stdout=sys.stdout)
42
while not step > traciEndTime:
43
traci.simulationStep()
44
vehs = traci.vehicle.getIDList()
45
if vehs.index("horiz") < 0 or len(vehs) > 3:
46
print("Something is wrong")
47
step += 1
48
49
50
print("----------- SUMO ends first -----------")
51
sys.stdout.flush()
52
for i in range(3):
53
print(" Run %s" % i)
54
runSingle(50, 99, i)
55
for i in range(3):
56
traci.switch(str(i))
57
print("Print ended at step %s" % traci.simulation.getTime())
58
traci.close()
59
try:
60
# should throw because everything is closed
61
traci.simulation.getTime()
62
except traci.FatalTraCIError as e:
63
print(e)
64
65
print("----------- TraCI ends first -----------")
66
sys.stdout.flush()
67
for i in range(3):
68
print(" Run %s" % i)
69
runSingle(101, 99, i)
70
for i in range(3):
71
traci.switch(str(i))
72
print("Print ended at step %s" % traci.simulation.getTime())
73
traci.close()
74
try:
75
# should throw because label isnot known
76
traci.switch(str(i))
77
except traci.TraCIException as e:
78
print(e)
79
80