-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_prewarmer.py
More file actions
24 lines (20 loc) · 900 Bytes
/
cache_prewarmer.py
File metadata and controls
24 lines (20 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import threading
from concurrent.futures import ThreadPoolExecutor
from typing import Iterable, Callable
from fingerprint_cache import get_fingerprint
def prewarm_cache(paths: Iterable[str], db_path: str, compute_func: Callable[[str], tuple[int | None, str | None]] | None = None, max_workers: int = 2):
"""Warm fingerprint cache for ``paths`` in background."""
if compute_func is None:
def compute_func(p: str) -> tuple[int | None, str | None]:
try:
import acoustid
return acoustid.fingerprint_file(p)
except Exception:
return None, None
def worker() -> None:
with ThreadPoolExecutor(max_workers=max_workers) as exe:
for p in paths:
exe.submit(get_fingerprint, p, db_path, compute_func)
t = threading.Thread(target=worker, daemon=True)
t.start()
return t