Padlock is a Python library that provides a lock through a single, simple interface and offers several backends (actually, for now, only one, until someone contributes another) so you can choose the backend that best fits your needs.
Returns a factory named by name. If **kwargs are provided, an instance will be constructed and the kwargs will be passed to the class desired.
For instance, to construct a cassandra lock:
>>> import padlock, pycassa >>> pool = pycassa.ConnectionPool('my_keyspace') >>> lock = padlock.get('cassandra', pool=pool, column_family='MyColumnFamily') >>> with lock: ... do_some_stuff() "stuff" >>>
|Parameters:||name (string) – The lock class desired (see Backends)|
|Return type:||an ILock|
Your average, run of the mill, generic lock interface.
Acquires the lock.
Releases the lock.
The exit functionality for context managers (catches exceptions, releases the lock and whatnot)
The Cassandra lock is a lock implemented in a cassandra row. It’s best used when also using cassandra for some other purpose and contention would likely not be an issue anyway (the lock provides the extra safety gear necessary to perform some operation).
To use the cassandra lock, instantiate using the padlock.get() function. You’ll also need a recent version of pycassa installed:
import padlock, pycassa pool = pycassa.ConnectionPool('my_keyspsace' with padlock.get('cassandra', pool=pool, column_family='my_column_family'): do_some_important_shit()
Success! Please read through the class documentation to get a feel of how to use the lock. It will likely be a little specific to your implementation.
Raised old, stale locks exist on a row and we don’t want them to have existed.
Raised when a lock is already taken on this row.
A lock that is implemented in a row of a Cassandra column family. It’s good to use this type of lock when you want to lock a single row in cassandra for some purpose in a scenario where there will not be a lot of lock contention.
The following paramters are optional and all come with defaults:
You can also provide the following keyword arguments which will be passed directly to the ColumnFamily constructor if you didn’t provide the instance yourself:
Acquire the lock on this row. It will then read immediatly from cassandra, potentially retrying, potentially sleeping the executing thread.
Used internally - fills out pycassa.batch.CfMutator with the necessary steps to acquire the lock.
Used internally - used to fill out a pycassa.batch.CfMutator with the necessary steps to release the lock.
Used internally - serialize a timeout value (a long) to be inserted into a cassandra as a string.
Return all columns in this row with the timeout value deserialized into a long
Used internally - deserialize a timeout value that was stored in cassandra as a string back into a long.
Allow this row to be locked by something (or someone) else. Performs a single write (round trip) to Cassandra.
Clean up after ourselves. Removes all lock columns (everything returned by read_lock_columns()) that is not stale.
|Parameters:||force – Remove even non-stale locks|
Used internally - return the current time, as microseconds from the unix epoch (Jan 1 1970 UTC)
Whether or not the lock can be verified by reading the row and ensuring the paramters of the lock according to the current CassandraDistributedRowLock instance’s configuration is valid.
This must only be called after acquire() is called, or else you will get a ValueError
|Parameters:||cur_time (long) – The current time in microseconds|