当前位置: 首页>>代码示例>>Python>>正文


Python Connection.lock方法代码示例

本文整理汇总了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)
开发者ID:sublee,项目名称:couchbase-python-client,代码行数:72,代码来源:libcouchbase.py


注:本文中的couchbase._libcouchbase.Connection.lock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。