Skip to content

Instantly share code, notes, and snippets.

@cameronmaske
Created March 16, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronmaske/742cd7daf74657b37fdc to your computer and use it in GitHub Desktop.
Save cameronmaske/742cd7daf74657b37fdc to your computer and use it in GitHub Desktop.

Contributing a new backend.

Contributing a new locking backend is greatly appreciated! Each new backend must implement the following methods...

class Backend(object):
    """
    Each new backend must implement the following methods,
    - __init__
    - raise_or_lock
    - clear_lock
    """
    def __init__(self, settings):
        pass 

    def raise_or_lock(self, key, timeout):
        pass 

    def clear_lock(self, key):
        pass

def raise_or_lock(self, key, timeout)

Checks if the task is locked based on the key argument (str). If already locked should raise an AlreadyQueued exception. If not, locks the task by the key. A timeout argument (int) can also be passed in. The key should be cleared after the timeout (in seconds) has passed.

def clear_lock(self, key)

Removes the lock based on the key argument (str). This is called after a task completes (either successfully, or fails beyond celery's retry limit).

def __init__(self, settings)

The settings argument (dict) is based on the celery once configuration. This can be used to setup the connection/client to the backend. Any imports for backend specific modules should happen inside here.

The redis backend is a good example of all of this in practice. If you'd like to contribute a new backend and still feel unsure how to do so, feel free to open an issue with any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment