Skip to content

Instantly share code, notes, and snippets.

@zbskii
Created January 22, 2014 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbskii/8551788 to your computer and use it in GitHub Desktop.
Save zbskii/8551788 to your computer and use it in GitHub Desktop.
Lockfile lib
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