from concurrent.futures import ThreadPoolExecutor from pathlib import Path import subprocess def task(name: str) -> subprocess.Popen: return subprocess.Popen(f'C:\\Windows\\System32\\cmd.exe /C "{name}"', stdout=subprocess.DEVNULL).wait() def run(base_dir: str, max: int = 5) -> None: files = [str(f) for f in Path(base_dir).glob("*.bat") if not f.name.startswith("_")] with ThreadPoolExecutor(max_workers=max) as executor: executor.map(task, files) if __name__ == "__main__": run("C:\\GlobalCube\\System\\OPTIMA\\SQL\\batch")