Really simple locking

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from datetime import datetime, timedelta

from django.db import models


class LockManager(models.Manager):
    """
    Usage:

        # Lock 'sync' for 2 minutes.
        if Lock.objects.lock('sync', 120):
            # This code will be accessed only
            # once per 2 minutes.

    """
    def get_lock(self, name):
        try:
            return self.get(name=name)
        except Lock.DoesNotExist:
            return Lock(name=name)

    def lock(self, name, seconds):
        """
        Set new lock and return True, but if already locked, do nothing and
        return False.

        ``name``
            Lock name, any string.

        ``seconds``
            For how many seconds ``name`` must be locked.
        """
        now = datetime.now()
        lock = self.get_lock(name)
        if lock.locked and lock.locked > now:
            return False
        else:
            lock.locked = now + timedelta(seconds=seconds)
            lock.save()
            return True


class Lock(models.Model):
    name = models.CharField(max_length=16, unique=True)
    locked = models.DateTimeField()

    objects = LockManager()

More like this

  1. lock tables decorator by seandong 5 years, 7 months ago
  2. Locking tables by miohtama 5 years, 9 months ago
  3. jQuery color picker model field by fneumann 5 years, 4 months ago
  4. Model Locking Mixin & Decorator (MySQL Advisory Locks) by pio 2 years, 11 months ago
  5. SmartyPants Filter by obeattie 7 years ago

Comments

adamlofts (on February 14, 2011):

I'm pretty sure the lock path is not thread safe. You'll need to be ready to catch a uniqueness exception on lock.save() if 2 threads try to create a lock at the same time.

#

sirex (on February 26, 2011):

@adamlofts:

Maybe lock method should go in one transaction?

#

(Forgotten your password?)