Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/multipleConnections/unspecifiedOrderWarning/runner.py
169727 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 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, orderOdd):
40
orderTime = 0.25
41
time.sleep(orderTime * index) # assure ordering of outputs
42
print("Starting process %s" % (index))
43
sys.stdout.flush()
44
time.sleep(orderTime * index) # assure ordering of outputs
45
step = 1
46
try:
47
traci.init(port)
48
if orderOdd and index % 2 == 1:
49
traci.setOrder(index)
50
sumoStop = False
51
while not step > traciEndTime:
52
traci.simulationStep()
53
vehs = traci.vehicle.getIDList()
54
if len(vehs) > 3:
55
print("Something is wrong")
56
step += 1
57
endTime = traci.simulation.getTime()
58
traci.close()
59
# ~ except traci.FatalTraCIError as e:
60
except Exception as e:
61
time.sleep(orderTime * index) # assure ordering of outputs
62
sumoStop = True
63
print("Process %s: " % index, str(e), " (at TraCIStep %s)" % step)
64
sys.stdout.flush()
65
if not sumoStop:
66
time.sleep(orderTime * index) # assure ordering of outputs
67
print("Process %s ended at step %s" % (index, endTime))
68
sys.stdout.flush()
69
70
71
def runSingle(sumoEndTime, traciEndTime, numClients, orderOdd=False):
72
fdi = open("sumo.sumocfg")
73
fdo = open("used.sumocfg", "w")
74
fdo.write(fdi.read() % {"end": sumoEndTime, "steplength": DELTA_T / 1000.})
75
fdi.close()
76
fdo.close()
77
sumoProcess = subprocess.Popen(
78
"%s -v --num-clients %s -c used.sumocfg -S -Q --remote-port %s" %
79
(sumoBinary, numClients, PORT), shell=True, stdout=sys.stdout) # Alternate ordering
80
procs = [multiprocessing.Process(target=traciLoop, args=(PORT, traciEndTime, i + 1, orderOdd))
81
for i in range(numClients)]
82
for p in procs:
83
p.start()
84
for p in procs:
85
p.join()
86
sumoProcess.wait()
87
sys.stdout.flush()
88
89
90
if __name__ == '__main__':
91
multiprocessing.set_start_method('spawn')
92
print("----------- Warning Test -----------")
93
print(" Run 1")
94
sys.stdout.flush()
95
runSingle(50, 99, 2)
96
print(" Run 2")
97
sys.stdout.flush()
98
runSingle(50, 99, 2, True)
99
100