runtraj.execution.inputs.lock#

runtraj.execution.inputs.lock(url, name)[source]#

Hold a lock on the file name at url while the with block runs.

Hold it around reading a file, changing it and writing it back, so a second program doing the same waits for the first instead of writing over its change. Each hour’s pod of a schedule holds it on releases.csv while it copies the file (get()), adds its hours, and copies it back (put()).

The lock is a small file .<name>.lock next to name, made only if it is not there (open(..., "x"): If-None-Match on S3, ifGenerationMatch=0 on Google Cloud Storage, O_EXCL on a folder), and deleted at the end of the block. It says which host and process holds it. While the block runs, the holder rewrites it every LOCK_REFRESH_SECONDS, so a long hold (a pod downloading the met) still looks alive. A program that finds it waits, and takes it over if it has not changed for LOCK_STALE_SECONDS by its own clock, so a holder that crashed does not stop every later one.

Parameters:
  • url (str) – Where the project’s inputs are: a URL, or a folder.

  • name (str) – The file to lock, relative to the project folder.

Raises:

OSError – If the lock cannot be made or read, such as a refused connection or expired credentials.

Return type:

Iterator[None]

Notes

A store that ignores If-None-Match (some older S3-compatible ones) lets two programs make the lock at once, so it does not protect there. A holder that stops rewriting its lock for LOCK_STALE_SECONDS (a process paused that long) can be taken over; it then logs a warning when it finishes.

Examples

>>> import fsspec
>>> fs = fsspec.filesystem("memory")
>>> with lock("memory://lock-example/project", "releases.csv"):
...     fs.exists("/lock-example/project/.releases.csv.lock")
True
>>> fs.exists("/lock-example/project/.releases.csv.lock")
False