Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: add pdfium_guard for thread-safe PDF document handling

myhloli934fbd03ecc1
diff --git a/mineru/utils/pdfium_guard.py b/mineru/utils/pdfium_guard.py
new file mode 100644
index 00000000..d2883724
--- /dev/null
+++ b/mineru/utils/pdfium_guard.py
@@ -0,0 +1,45 @@
+import asyncio
+import threading
+from contextlib import asynccontextmanager, contextmanager
+from typing import Any, Callable, TypeVar
+
+
+_pdfium_lock = threading.RLock()
+
+T = TypeVar("T")
+
+
+@contextmanager
+def pdfium_guard():
+ with _pdfium_lock:
+ yield
+
+
+@asynccontextmanager
+async def aio_pdfium_guard():
+ await asyncio.to_thread(_pdfium_lock.acquire)
+ try:
+ yield
+ finally:
+ _pdfium_lock.release()
+
+
+def open_pdfium_document(
+ opener: Callable[..., T],
+ *args: Any,
+ **kwargs: Any,
+) -> T:
+ with pdfium_guard():
+ return opener(*args, **kwargs)
+
+
+def get_pdfium_document_page_count(pdf_doc) -> int:
+ with pdfium_guard():
+ return len(pdf_doc)
+
+
+def close_pdfium_document(pdf_doc) -> None:
+ if pdf_doc is None:
+ return
+ with pdfium_guard():
+ pdf_doc.close()