24 lines
783 B
Python
24 lines
783 B
Python
"""Coordinate queue file reads and replacement across Windows processes/threads."""
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
from filelock import FileLock
|
|
|
|
|
|
@lru_cache(maxsize=128)
|
|
def _directory_lock(directory: Path) -> FileLock:
|
|
# One small OS-backed lock per queue directory, not one leftover file per
|
|
# task. FileLock releases the OS lock when its process exits.
|
|
return FileLock(directory / ".artemis-queue-io.lock", timeout=3)
|
|
|
|
|
|
def queue_file_lock(path: Path):
|
|
"""Hold only during file I/O, never across device operations or retry sleeps."""
|
|
return _directory_lock(path.parent.resolve()).acquire(poll_interval=0.005)
|
|
|
|
|
|
def read_queue_text(path: Path) -> str:
|
|
with queue_file_lock(path):
|
|
return path.read_text(encoding="utf-8")
|