Skip to content

Commit 49b03b9

Browse files
committed
feat(executor): spawn instances in parallel
1 parent 8d94917 commit 49b03b9

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/balatrollm/executor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ async def run(self) -> None:
4545
async def _start_instances(self, ports: range) -> None:
4646
"""Start Balatro instances."""
4747
cfg = BalatrobotConfig.from_env()
48+
49+
# Create all instances
50+
instances = []
4851
for port in ports:
4952
instance = BalatroInstance(cfg, port=port)
50-
await instance.start()
53+
instances.append((port, instance))
54+
55+
# Start all instances in parallel
56+
await asyncio.gather(*(instance.start() for _, instance in instances))
57+
58+
# Register instances in pool
59+
for port, instance in instances:
5160
self._instances[port] = instance
5261
await self._port_pool.put(port)
5362

@@ -71,7 +80,7 @@ async def run_task(task: Task) -> None:
7180
port = await self._port_pool.get()
7281
try:
7382
count += 1
74-
print(f"Running {task} ({count}/{total})")
83+
print(f"Running {task} ({count:0{len(str(total))}d}/{total})")
7584
bot = Bot(task=task, config=self.config, port=port)
7685
async with bot:
7786
await bot.play(self.runs_dir)
@@ -80,7 +89,7 @@ async def run_task(task: Task) -> None:
8089

8190
pending = [asyncio.create_task(run_task(t)) for t in self.tasks]
8291
try:
83-
await asyncio.gather(*pending)
92+
await asyncio.gather(*pending, return_exceptions=True)
8493
except asyncio.CancelledError:
8594
self._shutdown.set()
8695
for t in pending:

0 commit comments

Comments
 (0)