本文整理汇总了Python中couchbase._libcouchbase.Connection.lock方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.lock方法的具体用法?Python Connection.lock怎么用?Python Connection.lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类couchbase._libcouchbase.Connection
的用法示例。
在下文中一共展示了Connection.lock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lock
# 需要导入模块: from couchbase._libcouchbase import Connection [as 别名]
# 或者: from couchbase._libcouchbase.Connection import lock [as 别名]
def lock(self, key, ttl=0):
"""Lock and retrieve a key-value entry in Couchbase.
:param key: A string which is the key to lock.
:param int: a TTL for which the lock should be valid. If set to
`0` it will use the default lock timeout on the server.
While the lock is active, attempts to access the key (via
other :meth:`lock`, :meth:`set` or other mutation calls) will
fail with an :exc:`couchbase.exceptions.TemporaryFailError`
This function otherwise functions similarly to :meth:`get`;
specifically, it will return the value upon success.
Note the :attr:`~Result.cas` value from the :class:`Result`
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:`set` family of functions when the valid CAS is
supplied
:raise: :exc:`couchbase.exceptions.TemporaryFailError` if the key
was already locked.
:raise: See :meth:`get` for possible exceptions
Lock a key ::
rv = cb.lock("locked_key", ttl=100)
# This key is now locked for the next 100 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:`set` with CAS ::
rv = self.cb.lock("locked_key", ttl=100)
new_value = rv.value.upper()
cb.set("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")
except TemporaryFailError:
print("Key is currently locked.. waiting")
time.sleep(0)
if not rv:
raise Exception("Waited too long..")
# Do stuff..
cb.unlock("key", rv.cas)
.. seealso::
:meth:`get`
:meth:`lock_multi`
:meth:`unlock`
"""
return _Base.lock(self, key, ttl=ttl)