Skip to content

Instantly share code, notes, and snippets.

@clungzta
Created November 7, 2018 06:21
Show Gist options
  • Select an option

  • Save clungzta/09e7155225636e244f60291369e6ce4c to your computer and use it in GitHub Desktop.

Select an option

Save clungzta/09e7155225636e244f60291369e6ce4c to your computer and use it in GitHub Desktop.
import time
from lockfile import LockFile, LockTimeout
filepath = '/home/alex/Downloads/blah.txt'
lock = LockFile(filepath)
if lock.is_locked():
print('Waiting to acquire the file lock...')
lock.acquire(timeout=10)
with open(filepath, 'w') as f:
print('Acquired the file lock, now writing!')
f.writelines(['{}'.format(time.time())])
print('sleeping to simulate a slow file operation...')
time.sleep(10)
print("Successfully updated the file, unlocking...")
lock.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment