Lock or unlock
Takes or releases a named lock so only one run at a time works on a shared thing.
action the lock name
Keyboard Maestro: Semaphore
Details #
A semaphore is a named lock. Before touching something that two runs must not share at once, lock it. When you're done, unlock it. If another run holds the lock, yours waits for up to timeout seconds, then the step fails.
The name is free text. Any macros that use the same name compete for the same lock.
Locking a name this run already holds does nothing and doesn't wait. Unlocking a name this run doesn't hold does nothing either, and the Trace says so.
Locks are released for you when the run ends, however it ends, so a failed macro can't leave one stuck. A lock also expires when the holder's time limit would have.
- Wait for the lock “printer”
- Show “Using the printer”
- Release the lock “printer”
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
name | text | Yes | The lock's name. | |
action | one of | lock unlock reset | lock (default), unlock or reset. reset frees the lock whoever holds it. | |
timeout | number | Seconds to wait for a busy lock. Default 10. This value also sets the step's time limit unless the step has its own timeout, and a wait stops 0.3 seconds before that limit. So a long wait needs no second setting, but a short step timeout cuts the wait short. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Wait for the lock “sync”
- Show “Syncing”
- Release the lock “sync”
Gotchas #
A dry run simulates this step. Nothing is locked.
Locks live in memory. They don't survive a restart of the app, and they don't protect against other software.
For one macro that must never overlap itself, the macro's own concurrent setting (queue, cancelPrevious, parallel, ignore) is simpler than a lock.