nemo_gym.utils.install_lock
nemo_gym.utils.install_lock
Share one local tool installation among the worker processes of a server.
With num_workers > 1, each uvicorn worker installs a missing tool on its own first call,
and the workers’ first calls can arrive together.
Without coordination they would download, unpack and npm install into the same directory at once.
An installer takes install_lock around its work,
so one worker installs while the others wait and then reuse that install.
An install records where it put the tool, and which version it installed, only after it finished,
with record_install, so an interrupted install is never mistaken for a complete one.
A record is reused only for the version it names, so a changed pin reinstalls instead of running the old tool.
A waiter waits as long as the holder installs, with no timeout. A hung install already hung the worker that ran it, and a timeout would let a waiter install into the directory the holder may still be writing.
Module Contents
Functions
Data
API
Put a recorded install’s directory first on PATH when it holds executable at version.
Another worker may have installed the tool into a directory that is not on this process’s PATH.
Returns False when there is no record, the record is for another version (or predates versioned records),
or the recorded directory no longer holds the executable. The caller then installs version again.
install_lock for a coroutine, which waits without blocking the event loop.
It polls instead of waiting in a thread, so a cancelled waiter leaves no thread behind that could take the lock later. When the lock file cannot be created or locked, it logs a warning and runs the block without exclusion.
Hold an exclusive lock on lock_path across processes until the block exits.
The lock file must live outside any directory the install may remove, because a lock on a removed file no longer excludes a process that opens the path again. When the lock file cannot be created or locked, it logs a warning and runs the block without exclusion.
Record that an install of version finished and put its executables in bin_dir.
version is the version the caller asked for, None for an unpinned install.
The marker is replaced atomically, so a reader sees either no record or a complete one.
Where the record cannot be written, such as a read-only package directory, it logs a warning:
the install itself succeeded, and a later process without the record installs again.