Ich schrieb 2 Python-Skripte namens Runner.py und connect.py. Das Runner-Skript startet eine Verkehrssimulation mit einem bestimmten Port und der andere verbindet und kann Befehle senden. Beide Skripte funktionieren in meiner Python-IDE einwandfrei. Aber ich möchte beide Skripte von Java starten, um Daten zu erhalten.(Jython) Probleme beim Ausführen von Python-Skript von Java
Beide Skripts starten, aber in der connect.py tritt ein Fehler auf. Ich verstehe nicht, warum ich das Skript von der Python-IDE ausführen kann, aber nicht von meinem Java-Code. Hier sind die Codes aus den Python-Skripte:
runner.py
import sys
import subprocess
import os
PORT = 8873
class Runner:
__gui = None
def __init__(self, gui):
self.__gui = gui
print "Starting runner..."
def runLocal(self):
sumoBinary = os.path.abspath(os.curdir)
sumoBinary = sumoBinary.split('de.uniol.inf.is.odysseus.pgtaxi')[0]
sumoBinary = sumoBinary + 'de.uniol.inf.is.odysseus.pgtaxi\\sumo\\bin\\sumo-gui'
scenario = os.path.abspath(os.curdir)
scenario = sumoBinary.split('de.uniol.inf.is.odysseus.pgtaxi')[0]
scenario = scenario + 'de.uniol.inf.is.odysseus.pgtaxi\\scenario\\oldenburg.sumocfg'
sumoProcess = subprocess.Popen([sumoBinary, "-c", scenario, "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)
if __name__ == '__main__':
conn = Runner('None')
conn.runLocal()
connect.py
import sys
import os
PORT = 8873
class Connection:
__gui = None
def __init__(self, gui):
self.__gui = gui
print "Starting connect..."
def initTraci(self):
tools = os.path.abspath(os.curdir)
tools = tools.split('de.uniol.inf.is.odysseus.pgtaxi')[0]
tools = tools + 'de.uniol.inf.is.odysseus.pgtaxi\\sumo\\tools'
sys.path.append(tools)
import traci
traci.init(PORT)
step = 0
while step < 1000:
traci.simulationStep()
step += 1
traci.close()
sys.stdout.flush()
def getFreePort(self):
tools = os.path.abspath(os.curdir)
tools = tools.split('de.uniol.inf.is.odysseus.pgtaxi')[0]
tools = tools + 'de.uniol.inf.is.odysseus.pgtaxi\\sumo\\tools'
sys.path.append(tools)
import sumolib
PORT = sumolib.miscutils.getFreeSocketPort()
if __name__ == '__main__':
conn = Connection('None')
conn.initTraci()
Ich erhalte diese Ausnahme:
Exception in thread "MainThread" Traceback (most recent call last):
File "C:\Users\FEPREUSS\Desktop\PG\workspace\de.uniol.inf.is.odysseus.pgtaxi\src\de\uniol\inf\is\odysseus\pgtaxi\traci\traci4python\connect.py", line 44, in <module>
conn.initTraci()
File "C:\Users\FEPREUSS\Desktop\PG\workspace\de.uniol.inf.is.odysseus.pgtaxi\src\de\uniol\inf\is\odysseus\pgtaxi\traci\traci4python\connect.py", line 25, in initTraci
traci.init(PORT)
File "C:\Users\FEPREUSS\Desktop\PG\workspace\de.uniol.inf.is.odysseus.pgtaxi\sumo\tools\traci\__init__.py", line 65, in init
return getVersion()
File "C:\Users\FEPREUSS\Desktop\PG\workspace\de.uniol.inf.is.odysseus.pgtaxi\sumo\tools\traci\__init__.py", line 82, in getVersion
return _connections[""].getVersion()
AttributeError: 'NoneType' object has no attribute 'getVersion'
Und die beiden Methoden von der lib, die die exception verursachen:
def init(port=8813, numRetries=10, host="localhost", label="default"):
"""
Establish a connection to a TraCI-Server and store it under the given
label. This method is not thread-safe. It accesses the connection
pool concurrently.
"""
_connections[label] = connect(port, numRetries, host)
switch(label)
return getVersion()
def getVersion():
return _connections[""].getVersion()
Hoffe jemand kann mir helfen.
Die Fehlermeldung zeigt an, dass die Verbindung zu SUMO fehlgeschlagen ist. Es ist eine verwirrende Nachricht, und es gibt bereits ein Ticket, um diese http://sumo.dlr.de/trac.wsgi/ticket/2444 zu adressieren. Kannst du die genauen jython-Aufrufe, die du getan hast, zu der Frage hinzufügen und prüfen, ob Sumo wirklich von runner.py gestartet wurde (erscheint in der Prozessliste). – Michael
Ich habe die vollständige Klasse für die jython-Verbindung hinzugefügt. Sumo ist wirklich gestartet. Auch ich habe versucht, eine Verbindung zu einer Sumo-Anwendung herzustellen, die auf einem Server ausgeführt wird, und ich habe die gleiche Fehlermeldung erhalten. –
Wie stellen Sie sicher, dass Sumo bereits aktiv ist und zuhört, wenn Sie versuchen, eine Verbindung herzustellen? Haben Sie nach einiger Zeit versucht, eine Verbindung herzustellen? – Michael