WithLock

WithLock[File["path"],expr]

locks the file path, evaluates expr, then releases the file.

WithLock[LocalSymbol["name"],expr]

locks the local symbol name, evaluates expr, then releases the local symbol.

WithLock[var,expr]

locks the shared variable var, evaluates expr, then releases the shared variable.

Details and Options

  • WithLock locks a File or LocalSymbol by creating it. WithLock releases the File or LocalSymbol by deleting it.
  • If the lock object is already locked, WithLock will wait until the lock is released.
  • Locking is atomic; at most, one process can lock an object, thus ensuring it evaluates expr exclusively.
  • WithLock has attribute HoldAll.
  • The following options can be given:
  • TimeConstraint Infinityhow long to wait for the lock to become available
    ExpirationDate Nonewhen the lock should expire
    PersistenceTime Infinityhow long the lock should remain valid
  • If the lock cannot be obtained within the time given by TimeConstraint, WithLock returns $TimedOut and does not evaluate expr.
  • A lock that has reached its expiration date or lifetime will be considered unlocked by the next WithLock that attempts to lock it. ExpirationDate gives an absolute date/time for expiration; PersistenceTime gives a relative time.
  • WithLock[var,expr] uses a shared variable for locking among parallel subkernels. It is equivalent to CriticalSection[var,expr].
  • When used on different computers, lock files must reside on a shared file system available to all computers where WithLock is run.

Examples

open allclose all

Basic Examples  (2)

Prepare to access a file by many concurrent processes:

Protect concurrent access with a lock file in the same directory:

Use several parallel processes to try to write to the same file concurrently:

The data has been written (in a nondeterministic order) without interference among the processes:

Initialize a file by writing 0 into it:

Concurrently read the file and write an updated value:

All accesses have effectively been serialized:

Unprotected concurrent accesses may overwrite each other's values and even read partial content:

Scope  (3)

File Locking  (1)

Use a lock file to serialize a concurrent update operation:

LocalSymbol Locking  (1)

Use a local symbol as a lock:

Shared Variable Locking  (1)

Use a shared symbol as a lock in a parallel computation:

The last process to write the file wins; previous values are overwritten cleanly:

Options  (3)

ExpirationDate  (1)

A lock can be set to expire, to protect against the locking process being unable to release the lock:

A lock that was not released carries identifying information:

Once it has expired, the next WithLock to use it will break the lock:

PersistenceTime  (1)

A lock can be set to expire, to protect against the locking process being unable to release the lock:

A lock that was not released carries identifying information:

Once it has expired, the next WithLock to use it will break the lock:

TimeConstraint  (1)

If a lock is taken, WithLock will wait forever and will need to be aborted:

Give a finite timeout value to limit the time spent waiting for the lock to become available:

Properties & Relations  (2)

Using a shared variable in WithLock is equivalent to CriticalSection:

Update operations on local symbols are automatically protected from concurrency:

Possible Issues  (4)

If a file used as a lock already exists, the locking will hang:

This evaluation will not return until it is aborted:

Always use a nonexisting file as a lock:

If a local symbol used as a lock has a value, the locking will hang:

This evaluation will not return until it is aborted:

Always use a nonexisting local symbol as a lock:

Not all file systems support the required operation for local symbol locking:

File locks are supported in more cases:

Launch parallel kernels on the local and one remote machine:

Files and lock files created on a local file system may not be available on remote machines:

Concurrently write to file on all parallel subkernels:

Only the results from the local kernels are available in the file:

The results from the remote machine have been written to a file local to that machine:

Use a file and lock file on a shared file system:

Concurrently write to file on all parallel subkernels:

Now, all results are written to the same file:

Wolfram Research (2021), WithLock, Wolfram Language function, https://reference.wolfram.com/language/ref/WithLock.html.

Text

Wolfram Research (2021), WithLock, Wolfram Language function, https://reference.wolfram.com/language/ref/WithLock.html.

CMS

Wolfram Language. 2021. "WithLock." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithLock.html.

APA

Wolfram Language. (2021). WithLock. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithLock.html

BibTeX

@misc{reference.wolfram_2024_withlock, author="Wolfram Research", title="{WithLock}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/WithLock.html}", note=[Accessed: 10-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_withlock, organization={Wolfram Research}, title={WithLock}, year={2021}, url={https://reference.wolfram.com/language/ref/WithLock.html}, note=[Accessed: 10-November-2024 ]}