Lock

class consulate.api.lock.Lock(uri, adapter, session, datacenter=None, token=None)

Wrapper for easy KV locks. Keys are automatically prefixed with consulate/locks/. To change the prefix or remove it invoke the :meth:~consulate.api.lock.Lock.prefix` method.

Example:

import consulate

consul = consulate.Consul()
with consul.lock.acquire('my-key'):
    print('Locked: {}'.format(consul.lock.key))
    # Do stuff
Raises:LockError
__init__(uri, adapter, session, datacenter=None, token=None)

Create a new instance of the Lock

Parameters:
  • uri (str) – Base URI
  • adapter (consul.adapters.Request) – Request adapter
  • session (consul.api.session.Session) – Session endpoint instance
  • datacenter (str) – datacenter
  • token (str) – Access Token
acquire(*args, **kwds)

A context manager that allows you to acquire the lock, optionally passing in a key and/or value.

Parameters:
  • key (str) – The key to lock
  • value (str) – The value to set in the lock
Raises:

LockError

key

Return the lock key

Return type:str
prefix(value)

Override the path prefix for the lock key

Parameters:value (str) – The value to set the path prefix to