23 lines
682 B
Python
23 lines
682 B
Python
"""并发执行辅助:线程池 + 信号量,对应 TS SDK 的 ConcurrentLimiter(默认 10)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from typing import Any, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
_DEFAULT_CONCURRENCY = 10
|
|
|
|
|
|
def run_concurrent(
|
|
funcs: list[Callable[[], T]],
|
|
limit: int = _DEFAULT_CONCURRENCY,
|
|
) -> list[T]:
|
|
"""并发执行一批无参 callable(每个返回一个结果),保持输入顺序返回结果。"""
|
|
if not funcs:
|
|
return []
|
|
with ThreadPoolExecutor(max_workers=max(1, min(limit, len(funcs)))) as pool:
|
|
return list(pool.map(lambda fn: fn(), funcs))
|