Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/simpleflock.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ def __enter__(self):
elif self._timeout is not None and time.time() > (start_lock_search + self._timeout):
# Exceeded the user-specified timeout.
raise

# TODO It would be nice to avoid an arbitrary sleep here, but spinning
# without a delay is also undesirable.
time.sleep(0.1)

def __exit__(self, *args):
fcntl.flock(self._fd, fcntl.LOCK_UN)
os.close(self._fd)
self._fd = None

# Try to remove the lock file, but don't try too hard because it is
# unnecessary. This is mostly to help the user see whether a lock
# exists by examining the filesystem.
Expand All @@ -43,12 +39,13 @@ def __exit__(self, *args):
except:
pass

fcntl.flock(self._fd, fcntl.LOCK_UN)
os.close(self._fd)
self._fd = None

if __name__ == "__main__":
print "Acquiring lock..."
with SimpleFlock("locktest", 2):
print "Lock acquired."
time.sleep(3)
print "Lock released."