Lock and Unlock Functionality
Overview
When multiple processes or users try to access and modify the same resource (like a file, database record, or list) at the same time, it can lead to conflicts, inconsistent data, or even system errors.
The Lock and Unlock functionality helps prevent these problems by ensuring that only one process can work on a given resource at a time.
-
Lock: Reserves resources so that no other process can modify them until they are released.
-
Unlock: Releases the reserved resources so others can use them.
How It Works
- Acquire a lock (
R_lockorRMPRequest.lock):- You give the lock a unique name (
lockname). - If another lock with the same name already exists, your process will wait until it becomes available (up to a maximum timeout).
- Once the lock is granted, your process is the only one allowed to access the resource.
- You give the lock a unique name (
- Release the lock (
R_unlockorRMPRequest.unlock):- When your process is done, you should explicitly release the lock.
- If not released manually, the system will automatically release it after the configured expiration time (default: 1 hour).
Parameters
- lockName (string) – Unique identifier of the lock.
- waitTimeout (ms) – (Optional) Maximum time to wait for the lock if it is already in use (up to 40 seconds).
- execTimeout (sec) – (Optional) Maximum time the lock can remain active if not manually released (default: 1 hour).
Example (Freemarker)
https://docs.runmyprocess.com/API_Reference/FM/Request.html#R_lock
${R_lock("myLock", 1000, 40000)}
<!-- Perform operations safely on a shared resource -->
${R_unlock("myLock")}
Example (JavaScript)
https://docs.runmyprocess.com/API_Reference/RunMyScript/#rmprequestlock
var success = RMPRequest.lock("myLock", 10000);
if (success) {
// Safe access to shared resource
RMPRequest.unlock("myLock");
} else {
// Could not acquire lock
}
Example (Python)
https://docs.runmyprocess.com/API_Reference/RunMyScript_Py/#lock
vstate = "ok"
lock_test = RMPRequest.lock("test", 10000)
if lock_test:
// Safe access to shared resource
RMPRequest.unlock("test")
else:
// Could not acquire lock
state = "nok"
RMPData.setOutput(state)
Best Practices
- Always release locks as soon as you’re done.
- Use meaningful lock names (e.g.,
"updateOrders"instead of"lock1"). - Keep lock duration as short as possible to avoid blocking other processes.