Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: enhance PDF rendering support for Windows and improve process termination handling
myhloli33da7e81c179
diff --git a/docs/en/usage/cli_tools.md b/docs/en/usage/cli_tools.mdindex 4ced9e9d..dc394f87 100644--- a/docs/en/usage/cli_tools.md+++ b/docs/en/usage/cli_tools.md@@ -119,12 +119,12 @@ Here are the environment variables and their descriptions:- `MINERU_PDF_RENDER_TIMEOUT`:* Used to set the timeout (in seconds) for rendering PDFs to images.* Default is `300` seconds; you can set a different value via an environment variable to adjust the rendering timeout.- * Only effective on Linux and macOS systems.+ * Effective on Linux, macOS, and Windows.- `MINERU_PDF_RENDER_THREADS`:- * Used to set the number of threads used when rendering PDFs to images.- * Default is `4`; you can set a different value via an environment variable to adjust the number of threads for image rendering.- * Only effective on Linux and macOS systems.+ * Used to set the render worker concurrency used when rendering PDFs to images.+ * Default is `4`; you can set a different value via an environment variable to adjust render worker concurrency.+ * Effective on Linux, macOS, and Windows.- `MINERU_PROCESSING_WINDOW_SIZE`:* Used to control the processing window size, which affects memory use and throughput on large-document workloads.diff --git a/docs/zh/usage/cli_tools.md b/docs/zh/usage/cli_tools.mdindex 566e3769..885637f7 100644--- a/docs/zh/usage/cli_tools.md+++ b/docs/zh/usage/cli_tools.md@@ -113,12 +113,12 @@ MinerU命令行工具的某些参数存在相同功能的环境变量配置,- `MINERU_PDF_RENDER_TIMEOUT`:* 用于设置将PDF渲染为图片的超时时间(秒)* 默认为`300`秒,可通过环境变量设置为其他值以调整渲染图片的超时时间。- * 仅在linux和macOS系统中生效。+ * 在 Linux、macOS 和 Windows 系统中生效。- `MINERU_PDF_RENDER_THREADS`:- * 用于设置将PDF渲染为图片时使用的线程数- * 默认为`4`,可通过环境变量设置为其他值以调整渲染图片时的线程数。- * 仅在linux和macOS系统中生效。+ * 用于设置将PDF渲染为图片时使用的渲染 worker 并发数+ * 默认为`4`,可通过环境变量设置为其他值以调整渲染 worker 并发数。+ * 在 Linux、macOS 和 Windows 系统中生效。- `MINERU_PROCESSING_WINDOW_SIZE`:* 用于设置单次处理窗口大小,影响大文档处理时的内存占用和吞吐表现diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.pyindex e40d5111..7542a727 100644--- a/mineru/backend/hybrid/hybrid_analyze.py+++ b/mineru/backend/hybrid/hybrid_analyze.py@@ -25,7 +25,6 @@ from mineru.backend.vlm.vlm_analyze import (_maybe_enable_serial_execution,)from mineru.data.data_reader_writer import DataWriter-from mineru.utils.check_sys_env import is_windows_environmentfrom mineru.utils.config_reader import get_device, get_processing_window_sizefrom mineru.utils.enum_class import ImageType, NotExtractTypefrom mineru.utils.model_utils import crop_img, get_vram, clean_memory@@ -716,7 +715,7 @@ async def aio_doc_analyze(try:for window_index, window_start in enumerate(range(0, page_count, effective_window_size or 1)):window_end = min(page_count - 1, window_start + effective_window_size - 1)- if pdf_bytes is not None and not is_windows_environment():+ if pdf_bytes is not None:images_list = await aio_load_images_from_pdf_bytes_range(pdf_bytes,start_page_id=window_start,diff --git a/mineru/backend/vlm/vlm_analyze.py b/mineru/backend/vlm/vlm_analyze.pyindex 8dac967f..aa9e2a1a 100644--- a/mineru/backend/vlm/vlm_analyze.py+++ b/mineru/backend/vlm/vlm_analyze.py@@ -25,7 +25,6 @@ from mineru.utils.pdf_image_tools import (aio_load_images_from_pdf_bytes_range,load_images_from_pdf_doc,)-from ...utils.check_sys_env import is_windows_environmentfrom ...utils.check_sys_env import is_mac_os_version_supportedfrom ...utils.config_reader import get_device, get_processing_window_size@@ -535,7 +534,7 @@ async def aio_doc_analyze(try:for window_index, window_start in enumerate(range(0, page_count, effective_window_size or 1)):window_end = min(page_count - 1, window_start + effective_window_size - 1)- if pdf_bytes is not None and not is_windows_environment():+ if pdf_bytes is not None:images_list = await aio_load_images_from_pdf_bytes_range(pdf_bytes,start_page_id=window_start,diff --git a/mineru/utils/pdf_image_tools.py b/mineru/utils/pdf_image_tools.pyindex d233da9c..29dd73b7 100644--- a/mineru/utils/pdf_image_tools.py+++ b/mineru/utils/pdf_image_tools.py@@ -3,7 +3,6 @@ import asyncioimport atexitimport multiprocessingimport os-import signalimport threadingimport timefrom io import BytesIO@@ -36,6 +35,8 @@ DEFAULT_PDF_IMAGE_DPI = 200# DEFAULT_PDF_IMAGE_DPI = 144MAX_PDF_RENDER_PROCESSES = 4MIN_PAGES_PER_RENDER_PROCESS = 30+PDF_RENDER_TERMINATE_GRACE_PERIOD_SECONDS = 0.1+PDF_RENDER_KILL_JOIN_TIMEOUT_SECONDS = 0.1_pdf_render_executor: ProcessPoolExecutor | None = None_pdf_render_executor_lock = threading.Lock()@@ -188,8 +189,7 @@ def shutdown_pdf_render_executor() -> None:_pdf_render_executor = Noneif executor is not None:- _terminate_executor_processes(executor)- executor.shutdown(wait=False, cancel_futures=True)+ executor.shutdown(wait=True, cancel_futures=True)atexit.register(shutdown_pdf_render_executor)@@ -295,25 +295,47 @@ async def aio_load_images_from_pdf_bytes_range(def _terminate_executor_processes(executor):"""强制终止 ProcessPoolExecutor 中的所有子进程"""- if hasattr(executor, '_processes'):- for pid, process in executor._processes.items():- if process.is_alive():- try:- # 先发送 SIGTERM 允许优雅退出- os.kill(pid, signal.SIGTERM)- except (ProcessLookupError, OSError):- pass-- # 给子进程一点时间响应 SIGTERM- time.sleep(0.1)-- # 对仍然存活的进程发送 SIGKILL 强制终止- for pid, process in executor._processes.items():- if process.is_alive():- try:- os.kill(pid, signal.SIGKILL)- except (ProcessLookupError, OSError):- pass+ processes = list(getattr(executor, "_processes", {}).values())+ if not processes:+ return++ alive_processes = []+ for process in processes:+ if not process.is_alive():+ continue+ try:+ process.terminate()+ except Exception:+ pass+ alive_processes.append(process)++ deadline = time.monotonic() + PDF_RENDER_TERMINATE_GRACE_PERIOD_SECONDS+ for process in alive_processes:+ remaining = max(0.0, deadline - time.monotonic())+ try:+ process.join(timeout=remaining)+ except Exception:+ pass++ for process in alive_processes:+ if not process.is_alive():+ continue+ try:+ kill_process = getattr(process, "kill", None)+ if callable(kill_process):+ kill_process()+ else:+ process.terminate()+ except Exception:+ pass++ for process in alive_processes:+ if not process.is_alive():+ continue+ try:+ process.join(timeout=PDF_RENDER_KILL_JOIN_TIMEOUT_SECONDS)+ except Exception:+ passdef load_images_from_pdf_core(@@ -355,7 +377,7 @@ def load_images_from_pdf_doc(pdf_page_num = get_pdfium_document_page_count(pdf_doc)normalized_end_page_id = get_end_page_id(end_page_id, pdf_page_num)- if pdf_bytes is not None and not is_windows_environment():+ if pdf_bytes is not None:return _load_images_from_pdf_bytes_range(pdf_bytes,dpi=dpi,