import unittest
-from fcntl import flock, LOCK_EX, LOCK_UN
+from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
class struct(object):
'A simple namespace object.'
finally: rsrc.close()
return wrapper
-def with_lock_file(path):
+def with_lock_file(path, exclusive = True):
"""
Context manager for lock files. Example:
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