Login

Really simple locking

Author:
sirex
Posted:
February 14, 2011
Language:
Python
Version:
1.2
Score:
1 (after 1 ratings)

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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months 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?

#

Please login first before commenting.