@contextmanager
def flock(lockfile, shared=False):
 fcntl.flock(lockfile, (fcntl.LOCK_SH if shared else fcntl.LOCK_EX))
 try:
  (yield)
 finally:
  fcntl.flock(lockfile, fcntl.LOCK_UN)

@ contextmanager def flock ( lockfile , shared = False ) : fcntl . flock ( lockfile , ( fcntl . LOCK_SH if shared else fcntl . LOCK_EX ) ) try : ( yield ) finally : fcntl . flock ( lockfile , fcntl . LOCK_UN )
lock a file object using flock(2) for the duration of a with statement .