Skip to content

Commit

Permalink
Set "remote" as default.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamenot committed Jan 25, 2024
1 parent ab31e24 commit 7627672
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion smarts/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _convert_truthy(t: str) -> bool:
("ray", "log_to_driver"): False,
("sumo", "server_port"): 62232,
("sumo", "server_host"): "localhost",
("sumo", "serve_mode"): "local", # local|remote
("sumo", "serve_mode"): "remote", # local|remote
}


Expand Down
22 changes: 15 additions & 7 deletions smarts/core/utils/sumo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from __future__ import annotations

import argparse
import asyncio
import asyncio.streams
import json
Expand Down Expand Up @@ -166,9 +167,9 @@ def spawn_if_not(remote_host: str, remote_port: int):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client_socket.connect((remote_host, remote_port))
except socket.error:
except (OSError):
if remote_host in ("localhost", "127.0.0.1"):
command = ["python", "-m", __name__]
command = ["python", "-m", __name__, "--timeout", "600"]

# Use subprocess.Popen to start the process in the background
_ = subprocess.Popen(
Expand All @@ -177,11 +178,9 @@ def spawn_if_not(remote_host: str, remote_port: int):
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=False,
close_fds=True,
)
# Wait for process to start
client_socket.settimeout(5.0)
client_socket.connect((remote_host, remote_port))
client_socket.close()

else:
client_socket.close()

Expand All @@ -199,4 +198,13 @@ def main(*_, _host=None, _port=None, timeout: Optional[float] = 10 * 60):


if __name__ == "__main__":
main(timeout=None)
parser = argparse.ArgumentParser(__name__)
parser.add_argument(
"--timeout",
help="Duration of time until server shuts down if not in use.",
type=float,
default=None,
)
args = parser.parse_args()

main(timeout=args.timeout)
17 changes: 16 additions & 1 deletion smarts/core/utils/sumo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import socket
import subprocess
import sys
import time
from typing import Any, List, Literal, Optional, Tuple

from smarts.core.utils import networking
Expand Down Expand Up @@ -150,7 +151,21 @@ def generate(
self, base_params: List[str], sumo_binary: Literal["sumo", "sumo-gui"] = "sumo"
):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((self._remote_host, self._remote_port))

# Wait on server to start if it needs to.
error = None
for _ in range(5):
try:
client_socket.connect((self._remote_host, self._remote_port))
except OSError as err:
time.sleep(0.1)
error = err
continue
break
else:
raise error or OSError(
f"Unable to connect to server {self._remote_host}:{self._remote_port}"
)

client_socket.send(f"{sumo_binary}:{json.dumps(base_params)}\n".encode("utf-8"))

Expand Down

0 comments on commit 7627672

Please sign in to comment.