Skip to content

Commit c339f29

Browse files
committed
Fix max_tasks_per_child compatibility for Python < 3.11
1 parent 6ea793f commit c339f29

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

openevolve/process_parallel.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,28 @@ def start(self) -> None:
342342

343343
# Pass current environment to worker processes
344344
import os
345+
import sys
345346

346347
current_env = dict(os.environ)
347348

348-
# Create process pool with initializer
349-
self.executor = ProcessPoolExecutor(
350-
max_workers=self.num_workers,
351-
initializer=_worker_init,
352-
initargs=(config_dict, self.evaluation_file, current_env),
353-
max_tasks_per_child=self.config.max_tasks_per_child,
354-
)
349+
executor_kwargs = {
350+
"max_workers": self.num_workers,
351+
"initializer": _worker_init,
352+
"initargs": (config_dict, self.evaluation_file, current_env),
353+
}
354+
if sys.version_info >= (3, 11):
355+
logger.info(f"Set max {self.config.max_tasks_per_child} tasks per child")
356+
executor_kwargs["max_tasks_per_child"] = self.config.max_tasks_per_child
357+
elif self.config.max_tasks_per_child is not None:
358+
logger.warn(
359+
"max_tasks_per_child is only supported in Python 3.11+. "
360+
"Ignoring max_tasks_per_child and using spawn start method."
361+
)
362+
executor_kwargs["mp_context"] = mp.get_context("spawn")
355363

356-
logger.info(
357-
f"Started process pool with {self.num_workers} processes "
358-
f"and max {self.config.max_tasks_per_child} tasks per child"
359-
)
364+
# Create process pool with initializer
365+
self.executor = ProcessPoolExecutor(**executor_kwargs)
366+
logger.info(f"Started process pool with {self.num_workers} processes")
360367

361368
def stop(self) -> None:
362369
"""Stop the process pool"""

0 commit comments

Comments
 (0)