class Locker object _lock threading Lock def lock self func def method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Locker(object):
_lock = threading.Lock()
def lock(self, func):
def method(self, *args, **kwargs):
self._lock.acquire()
result = func(self, *args, **kwargs)
self.lock.release()
return result
return method
class Manager(Locker, object):
@self.lock
def get_task(self):
time_touched_limit = (datetime.now() - timedelta(seconds=Domain.time_touched_timeout))
try:
domain = Domain.objects.filter(time_touched__lt=time_touched_limit)[0]
except IndexError:
domain = None
else:
domain.time_touched = datetime.now()
domain.save()
return domain