Spaces:
Running
Running
Download evaluation/benchmark/locking.py from realdexter/RepoReaper: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/realdexter/RepoReaper/resolve/main/evaluation/benchmark/locking.py
- Command line
-
hf download hf://spaces/realdexter/RepoReaper/evaluation/benchmark/locking.py
-
curl -L -o locking.py https://huggingface.co/spaces/realdexter/RepoReaper/resolve/main/evaluation/benchmark/locking.py
1.14 kB
| """Single orchestrator per run; the usage ledger additionally locks each reservation.""" | |
| import fcntl | |
| import inspect | |
| from contextlib import contextmanager | |
| from functools import wraps | |
| from pathlib import Path | |
| def run_lock(directory): | |
| directory = Path(directory) | |
| directory.mkdir(parents=True, exist_ok=True) | |
| with (directory / ".run.lock").open("a") as handle: | |
| try: | |
| fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
| except BlockingIOError as exc: | |
| raise RuntimeError("Another generation/judge process owns this run") from exc | |
| try: | |
| yield | |
| finally: | |
| fcntl.flock(handle, fcntl.LOCK_UN) | |
| def exclusive_run(function): | |
| if inspect.iscoroutinefunction(function): | |
| async def call(directory, *args, **kwargs): | |
| with run_lock(directory): | |
| return await function(directory, *args, **kwargs) | |
| else: | |
| def call(directory, *args, **kwargs): | |
| with run_lock(directory): | |
| return function(directory, *args, **kwargs) | |
| return call | |