class Locker object def __init__ self args kwargs self lock threading

 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
class Locker(object):
def __init__(self, *args, **kwargs):
self.lock = threading.Lock()
def __call__(self, func):
def method(*args, **kwargs):
self.lock.acquire()
result = func(*args, **kwargs)
self.lock.release()
return result
return method
locker = Locker()
class Manager(Locker):
@locker
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