Repository: numpy/numpy
File: tools/ci/check_c_api_usage.py (Python)

Code before:
    excludes = set(args.exclude) if args.exclude else set(default_excludes)

    root = Path(args.root)
    if not root.exists():
        print(f"error: root '{root}' does not exist", file=sys.stderr)
        return 2

    files = sorted(iter_source_files(root, exts, excludes), key=str)
    print(f"Scanning {len(files)} C/C++ source files...")

    # Output file (mirrors your shell behavior)
    tmpdir = Path(".tmp")
    tmpdir.mkdir(exist_ok=True)

    findings = 0
    with tempfile.NamedTemporaryFile(
        prefix="c_api_usage_report.",
        suffix=".txt",
        dir=tmpdir,
        mode="w+",
        encoding="utf-8",
        delete=False,
        ) as out:
        report_path = Path(out.name)
        out.write("Running Suspicious C API usage report workflow...\n\n")
        for p in files:
            for func, lineno, pstr, raw in scan_file(p, func_rx, noqa_markers):
                findings += 1
                out.write(f"Found suspicious call to {func} in file: {pstr}\n")
                out.write(f" -> {pstr}:{lineno}:{raw}\n")
                out.write("Recommendation:\n")
                out.write(
                    "If this use is intentional and safe, add "
                    "'// noqa: borrowed-ref OK' on the same line "
                    "to silence this warning.\n"
                )
                out.write(
                    "Otherwise, consider replacing the call "
                    "with a thread-safe API function.\n\n")

        if findings == 0:
            out.write("C API borrow-ref linter found no issues.\n")

        # Echo report and set exit status
        out.flush()
        out.seek(0)
        sys.stdout.write(out.read())
    print(f"Report written to: {report_path}")

    return 1 if findings else 0


Review comment (suggestion):
i would replace the sequential `for p in files:` loop with parallel execution, e.g., using `concurrent.futures.ThreadPoolExecutor` to run `scan_file` on each file concurrently

Code after:
    )
    ap.add_argument(
        "--exclude",
        action="append",
        default=None,
        help=f"Directory name(s) to exclude (repeatable). Default: {default_excludes}",
    )
    args = ap.parse_args(argv)

    if args.ext:
        exts = {e if e.startswith(".") else f".{e}" for e in args.ext}
    else:
        exts = set(default_exts)
    excludes = set(args.exclude) if args.exclude else set(default_excludes)

    root = Path(args.root)
    if not root.exists():
        print(f"error: root '{root}' does not exist", file=sys.stderr)
        return 2

    files = sorted(iter_source_files(root, exts, excludes), key=str)

    # Determine concurrency: auto picks a reasonable cap for I/O-bound work
    if args.jobs is None or args.jobs <= 0:
        max_workers = min(32, (os.cpu_count() or 1) * 5)
    else:
        max_workers = max(1, args.jobs)
    print(f'Scanning {len(files)} C/C++ source files...\n')

    # Output file (mirrors your shell behavior)
    tmpdir = Path(".tmp")
    tmpdir.mkdir(exist_ok=True)

    findings = 0

    # Run the scanning in parallel; only the main thread writes the report
    all_hits: list[tuple[str, int, str, str]] = []
    if max_workers == 1:
        for p in files:
            all_hits.extend(scan_file(p, func_rx, noqa_markers))
    else:
        with ThreadPoolExecutor(max_workers=max_workers) as ex:
            fut_to_file = {ex.submit(scan_file, p, func_rx, noqa_markers):
                           p for p in files}
            for fut in as_completed(fut_to_file):
                try:
                    all_hits.extend(fut.result())
                except Exception as e:
                    print(f'Failed to scan {fut_to_file[fut]}: {e}')

    # Sort for deterministic output: by path, then line number