Writing a Backend¶
What is a backend¶
A backend is simply a store for objects. It needs to support four operations:
Saving Python objects associated with a key (a key is a string)
Loading Python objects by their key
Create a lock (identified by a key)
Release a lock (identified by a key)
There are a few other operations, like deleting an entry, that are useful, but not strictly necessary.
Backends are identified by a URI type string, generically called jugdir
.
For example, to connect to a redis backend, use redis:host:port/database
.
Your backend should support a similar scheme.
How to write a backend¶
You can start with the file jug/backends/base.py
which provides a
template with documentation. Implement the functions in there.
This module details all the operations that are necessary to implement a jug backend.
It can be used as a starting point (template) for writing new backends.
- class jug.backends.base.base_lock¶
- abstract fail()¶
Mark a task as failed. Should have no effect if the task isn’t locked
- Returns:
- failedbool
Whether the task was marked as failed
- abstract get()¶
Try to atomically create a lock
- Parameters:
- None
- Returns:
- lockedbool
Whether the lock was created
- abstract is_failed()¶
Returns whether this task is marked as failed.
This code is not race-condition free. It may happen that by the time this function returns, the failed lock has been released.
- Returns:
- failedboolean
- abstract is_locked()¶
Returns whether a lock exists for name. Note that the answer can be invalid by the time this function returns. Only by trying to acquire the lock can you avoid race-conditions. See the get() function.
This function is provided only because it might be possible to have a fast check before calling the expensive locking operation.
- Returns:
- lockedboolean
- abstract release()¶
Releases lock