Read works through files a batch at a time and keeps each batch
This commit is contained in:
+10
-5
@@ -47,6 +47,7 @@ from core.pipeline import (
|
|||||||
from persistance import config_log, files, nodes, silver
|
from persistance import config_log, files, nodes, silver
|
||||||
|
|
||||||
MIN_WORDS = 5 # fewer than this from a PDF page means it has no text layer
|
MIN_WORDS = 5 # fewer than this from a PDF page means it has no text layer
|
||||||
|
READ_BATCH = 32 # files read at a time (a read is kept as soon as its batch is done)
|
||||||
READ_COLUMNS = { # declared, so a load where every file read cleanly still has `error`
|
READ_COLUMNS = { # declared, so a load where every file read cleanly still has `error`
|
||||||
"text": {"data_type": "text", "nullable": True},
|
"text": {"data_type": "text", "nullable": True},
|
||||||
"ocr": {"data_type": "bool"},
|
"ocr": {"data_type": "bool"},
|
||||||
@@ -145,11 +146,15 @@ def run_read(p: str, node: Read):
|
|||||||
found[k]["format"] = file_format(files.content(k) or b"")
|
found[k]["format"] = file_format(files.content(k) or b"")
|
||||||
nodes.remember("read_cache", [{"key": k, **{c: found[k][c] for c in READ_COLUMNS}}
|
nodes.remember("read_cache", [{"key": k, **{c: found[k][c] for c in READ_COLUMNS}}
|
||||||
for k in legacy], READ_COLUMNS) # fmt: skip
|
for k in legacy], READ_COLUMNS) # fmt: skip
|
||||||
new = {k: files.content(k) for k in keys - set(found)}
|
new = sorted(keys - set(found))
|
||||||
with ProcessPoolExecutor() as pool:
|
with ProcessPoolExecutor() as pool: # a batch of files in memory at a time, each kept
|
||||||
read = dict(zip(new, pool.map(read_file, new.values()), strict=True))
|
for start in range(0, len(new), READ_BATCH):
|
||||||
nodes.remember("read_cache", [{"key": k, **r} for k, r in read.items()], READ_COLUMNS)
|
batch = new[start : start + READ_BATCH]
|
||||||
found |= read
|
read = dict(zip(batch, pool.map(read_file, (files.content(k) for k in batch)),
|
||||||
|
strict=True)) # fmt: skip
|
||||||
|
nodes.remember("read_cache", [{"key": k, **r} for k, r in read.items()],
|
||||||
|
READ_COLUMNS) # fmt: skip
|
||||||
|
found |= read
|
||||||
for row in rows:
|
for row in rows:
|
||||||
result = found.get(row.pop("key"), {"text": None, "ocr": False, "error": "empty"})
|
result = found.get(row.pop("key"), {"text": None, "ocr": False, "error": "empty"})
|
||||||
row |= {k: result[k] for k in READ_COLUMNS}
|
row |= {k: result[k] for k in READ_COLUMNS}
|
||||||
|
|||||||
Reference in New Issue
Block a user