From 9ff7ab491ca29d4c3077e2d2b04a4ae1cc48b1c9 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 8 Dec 2025 23:25:24 -0800 Subject: [PATCH] fix: handle None jobs in hOCR pipeline concurrency Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) refactor: remove unused argparse import from pdf_to_hocr.py Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) fix: import OCROptions in pdf_to_hocr pipeline Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) fix: handle None jobs in hocr_to_ocr_pdf pipeline Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) --- src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py | 8 +++++--- src/ocrmypdf/_pipelines/pdf_to_hocr.py | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py index c6fb5e38..c7dfdfc9 100644 --- a/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py +++ b/src/ocrmypdf/_pipelines/hocr_to_ocr_pdf.py @@ -6,7 +6,6 @@ from __future__ import annotations -import argparse import logging import logging.handlers from collections.abc import Sequence @@ -17,6 +16,7 @@ import PIL from ocrmypdf._concurrent import Executor from ocrmypdf._graft import OcrGrafter from ocrmypdf._jobcontext import PageContext, PdfContext +from ocrmypdf._options import OCROptions from ocrmypdf._pipeline import ( copy_final, render_hocr_page, @@ -34,6 +34,7 @@ from ocrmypdf._pipelines._common import ( from ocrmypdf._plugin_manager import OcrmypdfPluginManager from ocrmypdf._progressbar import ProgressBar from ocrmypdf.exceptions import ExitCode +from ocrmypdf.helpers import available_cpu_count log = logging.getLogger(__name__) @@ -55,7 +56,8 @@ def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[st """Convert hOCR files to OCR PDF.""" # Run exec_page_sync on every page options = context.options - max_workers = min(len(context.pdfinfo), options.jobs) + jobs = options.jobs or available_cpu_count() + max_workers = min(len(context.pdfinfo), jobs) if max_workers > 1: log.info("Continue processing %d pages concurrently", max_workers) @@ -105,7 +107,7 @@ def exec_hocr_to_ocr_pdf(context: PdfContext, executor: Executor) -> Sequence[st def run_hocr_to_ocr_pdf_pipeline( - options: argparse.Namespace, + options: OCROptions, *, plugin_manager: OcrmypdfPluginManager, ) -> ExitCode: diff --git a/src/ocrmypdf/_pipelines/pdf_to_hocr.py b/src/ocrmypdf/_pipelines/pdf_to_hocr.py index e87de0c3..c6c13c3a 100644 --- a/src/ocrmypdf/_pipelines/pdf_to_hocr.py +++ b/src/ocrmypdf/_pipelines/pdf_to_hocr.py @@ -6,7 +6,6 @@ from __future__ import annotations -import argparse import logging import logging.handlers import shutil @@ -16,6 +15,7 @@ import PIL from ocrmypdf._concurrent import Executor from ocrmypdf._jobcontext import PageContext, PdfContext +from ocrmypdf._options import OCROptions from ocrmypdf._pipeline import ( is_ocr_required, ocr_engine_hocr, @@ -31,6 +31,7 @@ from ocrmypdf._pipelines._common import ( worker_init, ) from ocrmypdf._plugin_manager import OcrmypdfPluginManager +from ocrmypdf.helpers import available_cpu_count log = logging.getLogger(__name__) @@ -61,7 +62,8 @@ def exec_pdf_to_hocr(context: PdfContext, executor: Executor) -> None: """Execute the OCR pipeline concurrently and output hOCR.""" # Run exec_page_sync on every page options = context.options - max_workers = min(len(context.pdfinfo), options.jobs) + jobs = options.jobs or available_cpu_count() + max_workers = min(len(context.pdfinfo), jobs) if max_workers > 1: log.info("Start processing %d pages concurrently", max_workers) @@ -82,7 +84,7 @@ def exec_pdf_to_hocr(context: PdfContext, executor: Executor) -> None: def run_hocr_pipeline( - options: argparse.Namespace, + options: OCROptions, *, plugin_manager: OcrmypdfPluginManager, ) -> None: