Last active
August 29, 2015 14:08
-
-
Save oiuww09fn/d1f14224fd1854d90035 to your computer and use it in GitHub Desktop.
Python fcntl does not lock as expected[file lock]http://stackoverflow.com/questions/9907616/python-fcntl-does-not-lock-as-expected
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import fcntl | |
import sys | |
import time | |
def lockFile(f): | |
try: | |
fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
except IOError: | |
return False | |
return True | |
fp = open("/tmp/tmp.txt", 'w') | |
a = lockFile(fp) | |
if not a: | |
sys.exit(0) | |
else: | |
print("Locked the file...") | |
while 1: | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment