{"record":{"id":"536d2abd9a082efa","repo":"ocrmypdf/OCRmyPDF","slug":"could-not-get-cpu-count-assuming-one-1-cpu-use","errorCode":null,"errorMessage":"Could not get CPU count. Assuming one (1) CPU. Use -j N to set manually.","messagePattern":"Could not get CPU count\\. Assuming one \\(1\\) CPU\\. Use -j N to set manually\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/ocrmypdf/helpers.py","lineNumber":215,"sourceCode":"\n\ndef monotonic(seq: Sequence) -> bool:\n    \"\"\"Does this sequence increase monotonically?\"\"\"\n    return all(b > a for a, b in zip(seq, seq[1:], strict=False))\n\n\ndef page_number(input_file: os.PathLike) -> int:\n    \"\"\"Get one-based page number implied by filename (000002.pdf -> 2).\"\"\"\n    return int(Path(input_file).name[0:6])\n\n\ndef available_cpu_count() -> int:\n    \"\"\"Returns number of CPUs in the system.\"\"\"\n    try:\n        return multiprocessing.cpu_count()\n    except NotImplementedError:\n        pass\n    warnings.warn(\n        \"Could not get CPU count. Assuming one (1) CPU. Use -j N to set manually.\"\n    )\n    return 1\n\n\ndef is_file_writable(test_file: StrOrBytesPath) -> bool:\n    \"\"\"Intentionally racy test if target is writable.\n\n    We intend to write to the output file if and only if we succeed and\n    can replace it atomically. Before doing the OCR work, make sure\n    the location is writable.\n    \"\"\"\n    try:\n        p = Path(os.fsdecode(test_file))\n        if p.is_symlink():\n            p = p.resolve(strict=False)\n\n        # p.is_file() throws an exception in some cases","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/ocrmypdf/OCRmyPDF/blob/5074a0b0e109362422b768fd271ed84bf717c4ec/src/ocrmypdf/helpers.py#L197-L233","documentation":"When multiprocessing.cpu_count() raises NotImplementedError (some sandboxes/container runtimes, CI restricted environments), available_cpu_count() warns and falls back to 1 CPU. Processing will be slow (single-threaded) unless -j/--jobs is set.","triggerScenarios":"Running OCRmyPDF (or calling available_cpu_count()) in environments where os.cpu_count()/multiprocessing.cpu_count() is unavailable — certain restricted containers, FreeBSD jails, or platforms lacking CPU affinity info.","commonSituations":"Docker with restricted /proc, minimal CI runners, some virtualized/sandboxed environments; user notices very slow OCR throughput.","solutions":["Pass -j N (CLI) or jobs=N (API) to set worker count explicitly","Upgrade the container/runtime so /proc/cpuinfo is available","If calling the helper directly, wrap it and default to os.process_cpu_count() (Python 3.13+) or a config value"],"exampleFix":"# before\nocrmypdf.ocr('in.pdf', 'out.pdf')\n# after\nocrmypdf.ocr('in.pdf', 'out.pdf', jobs=4)","handlingStrategy":"fallback","validationCode":"import os\\njobs = os.process_cpu_count() if hasattr(os, 'process_cpu_count') else 4","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass jobs=/--jobs in containers and CI","Include CPU info (/proc) in sandboxed runtimes","Benchmark expected throughput to detect accidental single-CPU runs"],"tags":["cpu-count","containers","performance","environment"],"backgroundTag":"cpu-count-unavailable","analyzedSha":"5074a0b0e109362422b768fd271ed84bf717c4ec","analyzedAt":"2026-08-27T11:57:38.523Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}