Skip to content

Commit cf7e167

Browse files
authored
Merge pull request #252 from funstory-ai/dev
fix: add threading locks for concurrent access to pixmap generation
2 parents a3b3874 + 4d42096 commit cf7e167

File tree

8 files changed

+84
-68
lines changed

8 files changed

+84
-68
lines changed

babeldoc/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.15"
1+
__version__ = "0.3.16"

babeldoc/const.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
from pathlib import Path
55

6-
__version__ = "0.3.15"
6+
__version__ = "0.3.16"
77

88
CACHE_FOLDER = Path.home() / ".cache" / "babeldoc"
99

babeldoc/docvision/doclayout.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import platform
55
import re
6+
import threading
67
from collections.abc import Generator
78

89
import cv2
@@ -121,6 +122,7 @@ def __init__(self, model_path: str):
121122
model.SerializeToString(),
122123
providers=providers,
123124
)
125+
self.lock = threading.Lock()
124126

125127
@staticmethod
126128
def from_pretrained():
@@ -279,7 +281,8 @@ def handle_document(
279281
]:
280282
for page in pages:
281283
translate_config.raise_if_cancelled()
282-
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
284+
with self.lock:
285+
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
283286
image = np.fromstring(pix.samples, np.uint8).reshape(
284287
pix.height,
285288
pix.width,

babeldoc/docvision/rpc_doclayout.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
from concurrent.futures import ThreadPoolExecutor
34
from pathlib import Path
45

@@ -125,6 +126,7 @@ def __init__(self, host: str = "http://localhost:8000"):
125126
self.host = host
126127
self._stride = 32 # Default stride value
127128
self._names = ["text", "title", "list", "table", "figure"]
129+
self.lock = threading.Lock()
128130

129131
@property
130132
def stride(self) -> int:
@@ -253,7 +255,8 @@ def predict_page(
253255
self, page, mupdf_doc: pymupdf.Document, translate_config, save_debug_image
254256
):
255257
translate_config.raise_if_cancelled()
256-
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
258+
with self.lock:
259+
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
257260
image = np.fromstring(pix.samples, np.uint8).reshape(
258261
pix.height,
259262
pix.width,

babeldoc/docvision/table_detection/rapidocr.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import re
3+
import threading
34
from collections.abc import Generator
45

56
import cv2
@@ -97,6 +98,7 @@ def __init__(self):
9798
det_use_dml=self.use_dml,
9899
)
99100
self.names = {0: "table_text"}
101+
self.lock = threading.Lock()
100102

101103
@property
102104
def stride(self):
@@ -233,7 +235,8 @@ def handle_document(
233235
]:
234236
for page in pages:
235237
translate_config.raise_if_cancelled()
236-
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
238+
with self.lock:
239+
pix = mupdf_doc[page.page_number].get_pixmap(dpi=72)
237240
image = np.fromstring(pix.samples, np.uint8).reshape(
238241
pix.height,
239242
pix.width,

babeldoc/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from babeldoc.translation_config import WatermarkOutputMode
2424

2525
logger = logging.getLogger(__name__)
26-
__version__ = "0.3.15"
26+
__version__ = "0.3.16"
2727

2828

2929
def create_parser():

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "BabelDOC"
3-
version = "0.3.15"
3+
version = "0.3.16"
44
description = "Yet Another Document Translator"
55
license = "AGPL-3.0"
66
readme = "README.md"
@@ -148,7 +148,7 @@ pythonpath = [".", "src"]
148148
testpaths = ["tests"]
149149

150150
[bumpver]
151-
current_version = "0.3.15"
151+
current_version = "0.3.16"
152152
version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]"
153153

154154
[bumpver.file_patterns]

uv.lock

+67-60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)