blob: 65dfd68ba8b4aa30b1199607507306313bf7ca5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/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
os.close(fd)
os.unlink(lp)
return True
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"
|