Created
January 22, 2014 01:10
-
-
Save zbskii/8551788 to your computer and use it in GitHub Desktop.
Lockfile lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import fcntl | |
from contextlib import contextmanager | |
@contextmanager | |
def file_lock(lock_file): | |
if os.path.exists(lock_file): | |
raise RuntimeError("Lockfile exists: %s" % lock_file) | |
else: | |
with open(lock_file, 'w') as fp: | |
try: | |
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
yield | |
finally: | |
os.remove(lock_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment