-
Notifications
You must be signed in to change notification settings - Fork 0
/
locks.py
79 lines (66 loc) · 1.96 KB
/
locks.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
Quick implementation of optimistic locking on relational table rows
and pessimistic locking on external resources
'''
from django.db import models, OperationalError, IntegrityError
from contextlib import contextmanager
import time
class OptimisticallyLocked(models.Model):
'''Model protected by concurrent updates via optimistic locking
'''
version = models.IntegerField(default=1)
class Meta:
abstract = True
def save(self):
updates = {
field.name: getattr(self, field.name)
for field in self._meta.fields
if not field.primary_key
}
updates['version'] += 1
updated = self._meta.model.objects.filter(
id = self.id,
version = self.version,
).update(**updates)
if not updated:
raise OperationalError('{} could not be updated!'.format(self))
self.version += 1
return
class BankAccount(OptimisticallyLocked):
username = models.CharField(max_length=100, unique=True)
balance = models.IntegerField()
class PessimisticLock(models.Model):
'''Represents a pessimistic lock on some external resource
'''
name = models.CharField(max_length=100, unique=True)
@classmethod
def acquire(cls, name):
try:
cls.objects.create(
name=name,
)
except IntegrityError:
raise OperationalError(
'Could not acquire lock for: {}!'.format(name)
)
@classmethod
def release(cls, name):
cls.objects.filter(
name=name,
).delete()
@contextmanager
def lock(name):
'''prevent concurrent execution of a code block
'''
PessimisticLock.acquire(name)
try:
yield
finally:
PessimisticLock.release(name)
return
def write_to_disk():
'''example usage of locking
'''
with lock('diskwrite'):
print 'WRIIIITING TO DISKKKKK'
time.sleep(10)