Skip to content

Commit b50d00d

Browse files
committed
Enh: Remove wait time in battlesnake
Closes #39
1 parent 135c658 commit b50d00d

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

codeclash/games/battlesnake/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ def __init__(self, config, *, tournament_id: str, local_output_dir: Path):
2323
else:
2424
self.run_cmd_round += f" --{arg} {val}"
2525

26+
def _wait_for_ports(self, ports: list[int], timeout: float = 3.0) -> None:
27+
"""Wait for all ports to be available, up to timeout seconds."""
28+
start_time = time.time()
29+
while time.time() - start_time < timeout:
30+
for port in ports:
31+
result = self.environment.execute(f"nc -z 0.0.0.0 {port}")
32+
if result["returncode"] != 0:
33+
self.logger.debug(f"Port {port} is not ready")
34+
break
35+
else:
36+
# All ports are ready (loop completed without break)
37+
self.logger.info("All ports are ready")
38+
return
39+
40+
time.sleep(0.1)
41+
2642
def get_stats(self, result_outputs: list[str], agents: list[Player]) -> RoundStats:
2743
scores = {}
2844
for ro in result_outputs:
@@ -37,13 +53,16 @@ def get_stats(self, result_outputs: list[str], agents: list[Player]) -> RoundSta
3753

3854
def execute_round(self, agents: list[Player]) -> RoundData:
3955
cmd = []
56+
ports = []
4057
for idx, agent in enumerate(agents):
4158
port = 8001 + idx
59+
ports.append(port)
4260
# Start server in background - just add & to run in background!
4361
self.environment.execute(f"PORT={port} python main.py &", cwd=f"/{agent.name}")
4462
cmd.append(f"--url http://0.0.0.0:{port} -n {agent.name}")
4563

46-
time.sleep(3) # Give servers time to start
64+
# Wait for all servers to be ready (up to 3 seconds)
65+
self._wait_for_ports(ports)
4766

4867
try:
4968
log_outputs, result_outputs = [], []

0 commit comments

Comments
 (0)