sometimes,you need to lock one or more table to ensure the integrity of data.may be it's help to you.
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 | def require_lock(*tables):
def _lock(func):
def _do_lock(*args,**kws):
#lock tables
cursor = connection.cursor()
cursor.execute("LOCK TABLES %s WRITE" %' '.join(tables))
try:
result = func(*args,**kws)
except Exception,e:
raise Exception(e)
else:
return result
finally:
#unlock tables
cursor.execute("UNLOCK TABLES")
if cursor:cursor.close()
return _do_lock
return _lock
'''
example:
@require_lock(table_A,table_B)
fuc(args):
pass
'''
|
More like this
- New Snippet! by Antoliny0919 4 days, 18 hours ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 3 weeks ago
- get_object_or_none by azwdevops 6 months, 2 weeks ago
- Mask sensitive data from logger by agusmakmun 8 months, 1 week ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 10 months ago
Comments
I think, that it would be better and shoter, to use try…finally block, like that:
#
Please login first before commenting.