Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/repeatedConnection/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 subprocess
24
import sys
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import sumolib # noqa
29
import traci # noqa
30
31
PORT = sumolib.miscutils.getFreeSocketPort()
32
sumoBinary = sumolib.checkBinary(sys.argv[1])
33
34
35
def runSingle(sumoEndTime, traciEndTime):
36
fdi = open("sumo.sumocfg")
37
fdo = open("used.sumocfg", "w")
38
fdo.write(fdi.read() % {"end": sumoEndTime})
39
fdi.close()
40
fdo.close()
41
step = 0
42
sumoProcess = subprocess.Popen(
43
"%s -c used.sumocfg -S -Q --remote-port %s" % (sumoBinary, PORT), shell=True, stdout=sys.stdout)
44
traci.init(PORT)
45
while not step > traciEndTime:
46
traci.simulationStep()
47
vehs = traci.vehicle.getIDList()
48
if vehs.index("horiz") < 0 or len(vehs) > 3:
49
print("Something is wrong")
50
step += 1
51
print("Print ended at step %s" % traci.simulation.getTime())
52
traci.close()
53
sumoProcess.wait()
54
sys.stdout.flush()
55
56
57
print("----------- SUMO ends first -----------")
58
sys.stdout.flush()
59
for i in range(0, 10):
60
print(" Run %s" % i)
61
runSingle(50, 99)
62
63
print("----------- TraCI ends first -----------")
64
sys.stdout.flush()
65
for i in range(0, 10):
66
print(" Run %s" % i)
67
runSingle(101, 99)
68
69