added shared/exclusive locking; added shared locking of initial JSON cache read
[invirt/packages/invirt-base.git] / files / usr / share / python-support / sipb-xen-base / invirt / common.py
index 5f669ba..3dc67a4 100644 (file)
@@ -39,7 +39,7 @@ def with_closing(rsrc):
         finally: rsrc.close()
     return wrapper
 
-def with_lock_file(path):
+def with_lock_file(path, exclusive = True):
     """
     Context manager for lock files.  Example:
 
@@ -53,7 +53,9 @@ def with_lock_file(path):
     def wrapper(func):
         @with_closing(file(path, 'w'))
         def g(f):
-            flock(f, LOCK_EX)
+            if exclusive: locktype = LOCK_EX
+            else:         locktype = LOCK_SH
+            flock(f, locktype)
             try: return func()
             finally: flock(f, LOCK_UN)
         return g