Skip to content

Commit 7355f2f

Browse files
committed
d
1 parent d38aab9 commit 7355f2f

File tree

2 files changed

+131
-127
lines changed

2 files changed

+131
-127
lines changed

openevolve/utils/async_utils.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,28 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
3333

3434

3535
async def run_with_timeout(
36-
coro: Callable, timeout: float, *args: Any, timeout_error_value: Any = None, **kwargs: Any
36+
coro: Callable,
37+
timeout: float,
38+
*args: Any,
39+
timeout_error_value: Any = None,
40+
**kwargs: Any
3741
) -> Any:
3842
"""
3943
Run a coroutine with a timeout, returning a default value on timeout
40-
44+
4145
Args:
4246
coro: Coroutine function to run
4347
timeout: Timeout in seconds
4448
*args: Arguments to pass to the coroutine
4549
timeout_error_value: Value to return on timeout (default: {"error": 0.0, "timeout": True})
4650
**kwargs: Keyword arguments to pass to the coroutine
47-
51+
4852
Returns:
4953
Result of the coroutine or timeout_error_value on timeout
5054
"""
5155
if timeout_error_value is None:
5256
timeout_error_value = {"error": 0.0, "timeout": True}
53-
57+
5458
try:
5559
return await asyncio.wait_for(coro(*args, **kwargs), timeout=timeout)
5660
except asyncio.TimeoutError:
@@ -59,24 +63,28 @@ async def run_with_timeout(
5963

6064

6165
async def run_sync_with_timeout(
62-
func: Callable, timeout: float, *args: Any, timeout_error_value: Any = None, **kwargs: Any
66+
func: Callable,
67+
timeout: float,
68+
*args: Any,
69+
timeout_error_value: Any = None,
70+
**kwargs: Any
6371
) -> Any:
6472
"""
6573
Run a synchronous function in an executor with a timeout
66-
74+
6775
Args:
6876
func: Synchronous function to run
6977
timeout: Timeout in seconds
7078
*args: Arguments to pass to the function
7179
timeout_error_value: Value to return on timeout (default: {"error": 0.0, "timeout": True})
7280
**kwargs: Keyword arguments to pass to the function
73-
81+
7482
Returns:
7583
Result of the function or timeout_error_value on timeout
7684
"""
7785
if timeout_error_value is None:
7886
timeout_error_value = {"error": 0.0, "timeout": True}
79-
87+
8088
try:
8189
loop = asyncio.get_event_loop()
8290
task = loop.run_in_executor(None, functools.partial(func, *args, **kwargs))
@@ -170,9 +178,17 @@ class TaskPool:
170178
"""
171179

172180
def __init__(self, max_concurrency: int = 10):
173-
self.semaphore = asyncio.Semaphore(max_concurrency)
181+
self.max_concurrency = max_concurrency
182+
self._semaphore: Optional[asyncio.Semaphore] = None
174183
self.tasks: List[asyncio.Task] = []
175184

185+
@property
186+
def semaphore(self) -> asyncio.Semaphore:
187+
"""Lazy-initialize the semaphore when first needed"""
188+
if self._semaphore is None:
189+
self._semaphore = asyncio.Semaphore(self.max_concurrency)
190+
return self._semaphore
191+
176192
async def run(self, coro: Callable, *args: Any, **kwargs: Any) -> Any:
177193
"""
178194
Run a coroutine in the pool

0 commit comments

Comments
 (0)