mne_bids.open_lock#

mne_bids.open_lock(path, *args, lock_timeout=None, lock=True, **kwargs)[source]#

Context manager that acquires a file lock with optional file opening.

If the filelock package is available, a lock is acquired on a lock file based on the given path (by appending ‘.lock’) and held for the whole with block. The lock file is removed when the lock is released.

If file opening arguments (*args, **kwargs) are provided, the file is opened in the specified mode. Otherwise, just the lock is acquired. A file opened with "w" or "wb" is written to a temporary file and moved into place at the end of the block, so readers never see it partially written.

The lock is re-entrant per thread: a nested call for the same path reuses the lock instead of waiting for it. To read a file and then write it, do both inside one with open_lock(path): block.

Parameters:
pathstr

The path to acquire a lock for (and optionally open).

*argstuple

Additional positional arguments forwarded to open.

lock_timeoutfloat | None

Timeout in seconds for acquiring the lock. If None, the default timeout applies.

lockbool

If True, use a FileLock (if available) to lock the file.

**kwargsdict

Additional keyword arguments forwarded to open.

Yields:
fidio.IOBase | None

File object if file opening args were provided, None otherwise.