|
10 | 10 | import time |
11 | 11 | import uuid |
12 | 12 | from pathlib import Path |
| 13 | +from concurrent.futures import ThreadPoolExecutor |
13 | 14 | from typing import Any, Dict, List, Optional, Union |
14 | 15 |
|
15 | 16 | from openevolve.config import Config, load_config |
@@ -616,29 +617,22 @@ def _maybe_evolve_prompts(self, iteration: int) -> None: |
616 | 617 | f"(score={template.score:.3f}, uses={template.uses})" |
617 | 618 | ) |
618 | 619 |
|
619 | | - # Create a sync wrapper for LLM generation that works in async context |
620 | | - # We use a thread pool to avoid event loop conflicts |
621 | | - import concurrent.futures |
622 | | - |
| 620 | + # Create a sync wrapper for LLM generation that works within an async context. |
| 621 | + # We run the async LLM call in a separate thread with its own event loop |
| 622 | + # to avoid conflicts with the main event loop. |
623 | 623 | def llm_generate_sync(system: str, user: str) -> str: |
624 | | - import asyncio |
625 | | - |
626 | | - # Create a new event loop in a thread to avoid conflicts |
627 | | - def run_in_new_loop(): |
628 | | - loop = asyncio.new_event_loop() |
629 | | - asyncio.set_event_loop(loop) |
630 | | - try: |
631 | | - return loop.run_until_complete( |
632 | | - self.llm_ensemble.generate_with_context( |
633 | | - system_message=system, |
634 | | - messages=[{"role": "user", "content": user}], |
635 | | - ) |
| 624 | + def _run_async_in_thread(): |
| 625 | + # asyncio.run() creates a new event loop, runs the coroutine, |
| 626 | + # and cleans up the loop automatically |
| 627 | + return asyncio.run( |
| 628 | + self.llm_ensemble.generate_with_context( |
| 629 | + system_message=system, |
| 630 | + messages=[{"role": "user", "content": user}], |
636 | 631 | ) |
637 | | - finally: |
638 | | - loop.close() |
| 632 | + ) |
639 | 633 |
|
640 | | - with concurrent.futures.ThreadPoolExecutor() as executor: |
641 | | - future = executor.submit(run_in_new_loop) |
| 634 | + with ThreadPoolExecutor(max_workers=1) as executor: |
| 635 | + future = executor.submit(_run_async_in_thread) |
642 | 636 | return future.result() |
643 | 637 |
|
644 | 638 | # Evolve the template |
|
0 commit comments