From 4f1f3b9b517440e45f46d81358dfe9fed251d3ed Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 21 Mar 2018 01:11:07 -0700 Subject: [PATCH] Move available_cpu_count to helpers --- ocrmypdf/__main__.py | 21 ++------------------- ocrmypdf/helpers.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/ocrmypdf/__main__.py b/ocrmypdf/__main__.py index 3cd37577..bfb956b6 100755 --- a/ocrmypdf/__main__.py +++ b/ocrmypdf/__main__.py @@ -23,7 +23,8 @@ import ruffus.proxy_logger as proxy_logger from .pipeline import JobContext, JobContextManager, \ cleanup_working_files, build_pipeline from .pdfa import file_claims_pdfa -from .helpers import is_iterable_notstr, re_symlink, is_file_writable +from .helpers import is_iterable_notstr, re_symlink, is_file_writable, \ + available_cpu_count from .exec import tesseract, qpdf, ghostscript from . import PROGRAM_NAME, VERSION @@ -531,24 +532,6 @@ def logging_factory(logger_name, logger_args): return root_logger -def available_cpu_count(): - try: - return multiprocessing.cpu_count() - except NotImplementedError: - pass - - try: - import psutil - return psutil.cpu_count() - except (ImportError, AttributeError): - pass - - complain( - "Could not get CPU count. Assuming one (1) CPU." - "Use -j N to set manually.") - return 1 - - def cleanup_ruffus_error_message(msg): msg = re.sub(r'\s+', r' ', msg) msg = re.sub(r"\((.+?)\)", r'\1', msg) diff --git a/ocrmypdf/helpers.py b/ocrmypdf/helpers.py index bb60d358..7e7ecd8b 100644 --- a/ocrmypdf/helpers.py +++ b/ocrmypdf/helpers.py @@ -6,6 +6,7 @@ from contextlib import suppress, contextmanager from pathlib import Path import sys import os +import multiprocessing def re_symlink(input_file, soft_link_name, log=None): @@ -57,6 +58,24 @@ def page_number(input_file): return int(os.path.basename(input_file)[0:6]) +def available_cpu_count(): + try: + return multiprocessing.cpu_count() + except NotImplementedError: + pass + + try: + import psutil + return psutil.cpu_count() + except (ImportError, AttributeError): + pass + + complain( + "Could not get CPU count. Assuming one (1) CPU." + "Use -j N to set manually.") + return 1 + + def is_file_writable(test_file): """Intentionally racy test if target is writable.