"""Batch processing utilities for parsing multiple ECG files."""from__future__importannotationsfromcollections.abcimportIteratorfromconcurrent.futuresimportProcessPoolExecutorfrompathlibimportPathfromecgdatakit.modelsimportECGRecordfromecgdatakit.parsing.parserimportFileParserdef_parse_single(file_path:Path)->ECGRecord:"""Parse a single file (top-level function for pickling in multiprocessing)."""returnFileParser().parse(file_path)
[docs]defparse_batch(files:list[Path|str],max_workers:int|None=None,)->Iterator[ECGRecord]:"""Parse multiple ECG files in parallel. Parameters ---------- files : list[Path | str] Paths to ECG files. max_workers : int | None Maximum number of worker processes. Defaults to CPU count. Yields ------ ECGRecord Parsed records in the same order as the input files. """paths=[Path(f)forfinfiles]withProcessPoolExecutor(max_workers=max_workers)aspool:yield frompool.map(_parse_single,paths)