Menu

Reject a Stale In-Memory Update with a Revision Check Inside the Lock
📰

Reject a Stale In-Memory Update with a Revision Check Inside the Lock

DEV Community: python·TokenPeak·2 days ago
#eJBg2yk7
#devtopython#devto#dev#self#cell
Reading 0:00
15s threshold

Two editors read the same value and independently prepare replacements. Serializing only the final assignment prevents overlapping writes but still permits a stale editor to overwrite the newer result. The acceptance condition has to be checked together with the assignment.

This example keeps an integer revision beside an immutable string payload. A replacement supplies the revision it read. Under one lock, the cell compares that expectation, either rejects the update or assigns the value, and advances the revision.

Compare and replace in one critical section

The Python lock reference describes primitive locking. The application-level revision rule here is additional policy. Every read and update must use the same cell interface; a caller directly editing its private attributes would violate the example's assumptions.

The public snapshot is a tuple containing an integer and a string, so it does not expose a nested mutable payload.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.