use*_*455 5 python multithreading google-cloud-datastore google-cloud-platform
推荐一种锁定实体(例如User(amount_earned))以在Google Cloud Datastore上执行一致的读取->更新->设置操作的方法。
使用memcache,我们通常执行memcache.add(lock_key),以确保只有进程/线程在修改对象。
由于内存缓存的添加是原子的,并且返回false,因此如果先前未添加密钥,则使用内存缓存可以轻松模拟“锁定”。
如何在Google Cloud数据存储区上模拟类似的锁定语义。
理想地
with gcp.lock(lock_key)as key:
user.get(user_id) # read
user.update(amount) # update
user.set() # save
# Release the lock, so that other processes can now updated user (identified by user_id) object
Run Code Online (Sandbox Code Playgroud)
小智 5
让我们以更新用户实体为例。为了使用乐观锁定,您必须使用特殊属性(例如,实体版本)。对于每次更新,版本字段将增加一。更新实体的步骤: