Skip to content

Instantly share code, notes, and snippets.

@Feuermurmel
Created February 13, 2017 10:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Feuermurmel/818af2649afd81aa7b56b6e584863474 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
import fcntl
@contextmanager
def lock_file(path):
with open(path, 'wb') as file:
fcntl.flock(file, fcntl.LOCK_EX)
try:
yield
finally:
fcntl.flock(file, fcntl.LOCK_UN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment