summaryrefslogtreecommitdiffstats
path: root/feed/sse_lock.py
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-11-23 18:35:19 +0000
committerLennart Poettering <lennart@poettering.net>2005-11-23 18:35:19 +0000
commit3971bc230008a576b9afa9eda47147d88a584776 (patch)
tree81ee529d872606b7279c9631b66eb133e0134ab5 /feed/sse_lock.py
parent3a24de286d8bb6a6fa5c802ad9587e15550f0801 (diff)
add proper locking for archives
git-svn-id: file:///home/lennart/svn/public/sse/trunk@44 5fbabb74-0606-0410-a5e4-b5cc6a42724e
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"
+
+
+
+
+