annotate web/atomicfile.py @ 225:f2838211f6ec

fixed point for old values
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Jan 2015 22:55:24 +0800
parents 6dd157a12035
children 87c20b8c5472
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import time
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import fcntl
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import sys
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 class AtomicFile(object):
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
7 DELAY = 0.5
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
8 def __init__(self, name):
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
9 self.name = name
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
11 def write(self, data, timeout = 5):
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
12 try:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
13 end = time.time() + timeout
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
14 with open(self.name, "r+") as f:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
15 while timeout == 0 or time.time() < end:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
16 try:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
17 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
18 except IOError:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
19 time.sleep(DELAY)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
20 continue
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
22 os.ftruncate(f.fileno(), 0)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
23 f.write(data)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
24 return True
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
26 except IOError, e:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
27 print>>sys.stderr, e
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
29 return False
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
31 def read(self, timeout = 5):
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
32 try:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
33 end = time.time() + timeout
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
34 with open(self.name, "r") as f:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
35 while timeout == 0 or time.time() < end:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
36 try:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
37 fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
38 except IOError:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
39 time.sleep(DELAY)
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
40 continue
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
42 return f.read()
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
44 except IOError, e:
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
45 print>>sys.stderr, e
191
8318d50d766d gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
202
6dd157a12035 Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
47 return None