Skip to content

Commit 6d17f51

Browse files
committed
Use filelock
Ensure that if have multiple concurrent requests don't clobber file. This is probably overkill since concurrent requests will actually interrupt one another w/ default server settings.
1 parent d793b6c commit 6d17f51

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import jinja2
2727
from jinja2.sandbox import ImmutableSandboxedEnvironment
28+
import filelock
2829

2930
import numpy as np
3031
import numpy.typing as npt
@@ -618,9 +619,14 @@ def chat_completion_handler(
618619
# slow down things at least a little (latency) because I/O is slow.
619620
if llama.formatted_prompt_path is not None:
620621
output_path = pathlib.Path(llama.formatted_prompt_path)
621-
with output_path.open("a", encoding="utf-8") as f:
622-
json.dump({"prompt": result.prompt, "prompt_tokens": prompt}, f)
623-
f.write("\n")
622+
623+
# We ensure that output path ends with .ndjson in pydantic validation.
624+
lockfile_path = output_path.with_suffix(".lock")
625+
lock = filelock.FileLock(str(lockfile_path))
626+
with lock:
627+
with output_path.open("a", encoding="utf-8") as f:
628+
json.dump({"prompt": result.prompt, "prompt_tokens": prompt}, f)
629+
f.write("\n")
624630

625631
if result.stop is not None:
626632
stop = [] if stop is None else [stop] if isinstance(stop, str) else stop

0 commit comments

Comments
 (0)