def get_and_lock(self, id, # type: str expiration, # type: int *options, # type: GetAndLockOptions **kwargs ): # type: (...)->GetResult final_options=forward_args(kwargs, *options) x = _Base.get(self.bucket, id, expiration, **final_options) _Base.lock(self.bucket, id, options) return ResultPrecursor(x, options)
def lock(self, # type: CBCollection key, # type: str *options, # type: LockOptions **kwargs # type: Any ): """Lock and retrieve a key-value entry in Couchbase. :param key: A string which is the key to lock. :param ttl: a TTL for which the lock should be valid. While the lock is active, attempts to access the key (via other :meth:`lock`, :meth:`upsert` or other mutation calls) will fail with an :exc:`.KeyExistsError`. Note that the value for this option is limited by the maximum allowable lock time determined by the server (currently, this is 30 seconds). If passed a higher value, the server will silently lower this to its maximum limit. This function otherwise functions similarly to :meth:`get`; specifically, it will return the value upon success. Note the :attr:`~.MutationResult.cas` value from the :class:`.MutationResult` object. This will be needed to :meth:`unlock` the key. Note the lock will also be implicitly released if modified by one of the :meth:`upsert` family of functions when the valid CAS is supplied :raise: :exc:`.TemporaryFailError` if the key is already locked. :raise: See :meth:`get` for possible exceptions Lock a key :: rv = cb.lock("locked_key", ttl=5) # This key is now locked for the next 5 seconds. # attempts to access this key will fail until the lock # is released. # do important stuff... cb.unlock("locked_key", rv.cas) Lock a key, implicitly unlocking with :meth:`upsert` with CAS :: rv = self.cb.lock("locked_key", ttl=5) new_value = rv.value.upper() cb.upsert("locked_key", new_value, rv.cas) Poll and Lock :: rv = None begin_time = time.time() while time.time() - begin_time < 15: try: rv = cb.lock("key", ttl=10) break except TemporaryFailError: print("Key is currently locked.. waiting") time.sleep(1) if not rv: raise Exception("Waited too long..") # Do stuff.. cb.unlock("key", rv.cas) .. seealso:: :meth:`get`, :meth:`unlock` """ final_options = forward_args(kwargs, *options) return _Base.lock(self.bucket, key, **final_options)