summaryrefslogtreecommitdiffstats
path: root/feed/sse_lock.py
diff options
context:
space:
mode:
Diffstat (limited to 'feed/sse_lock.py')
-rwxr-xr-xfeed/sse_lock.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/feed/sse_lock.py b/feed/sse_lock.py
new file mode 100755
index 0000000..8fd0459
--- /dev/null
+++ b/feed/sse_lock.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+import os, errno, time
+
+def try_lock_file(fn):
+
+ lp = "%s.%i" % (fn, os.getpid())
+
+ fd = os.open(lp, os.O_RDWR|os.O_CREAT, 0666)
+
+ try:
+ os.link(lp, fn)
+ except OSError, e:
+ os.close(fd)
+ os.unlink(lp)
+
+ if e.errno != errno.EEXIST:
+ raise e
+
+ return False
+
+ t = os.fstat(fd)
+ os.close(fd)
+ os.unlink(lp)
+
+ return t.st_nlink >= 2
+
+def unlock_file(fn):
+
+ os.unlink(fn)
+
+
+if __name__ == "__main__":
+
+ if not try_lock_file("test.lock"):
+ print "FAILED"
+ else:
+ try:
+ print "LOCKED"
+ time.sleep(60)
+ finally:
+ unlock_file("test.lock")
+
+ print "UNLOCKED"
+
+
+
+
+