import asyncio
from concurrent.futures import ThreadPoolExecutor

# Hanya 1 worker agar GPU/CPU tidak overload saat transkripsi berjalan paralel
_executor = ThreadPoolExecutor(max_workers=1)


async def run_in_threadpool(func, *args):
    """
    Jalankan fungsi synchronous (blocking) di thread pool
    agar tidak memblokir FastAPI event loop.

    Contoh pemakaian:
        result = await run_in_threadpool(transcribe_local, file_path, model_type)
    """
    loop = asyncio.get_running_loop()
    return await loop.run_in_executor(_executor, func, *args)