Created
May 25, 2018 04:58
-
-
Save baverman/f5d4acb1a2c27974747d36095b4b915a to your computer and use it in GitHub Desktop.
Revisions
-
baverman revised this gist
May 25, 2018 . No changes.There are no files selected for viewing
-
baverman created this gist
May 25, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ @contextmanager def lock(file_name, block=False): fp = open(file_name, 'w') opts = fcntl.LOCK_EX if not block: opts |= fcntl.LOCK_NB try: fcntl.lockf(fp, opts) except IOError: if block: raise yield False else: try: yield True finally: fcntl.lockf(fp, fcntl.LOCK_UN) fp.close()