annotate web/atomicfile.py @ 496:88204c0db5fa

Fridge setpoint as a float value
author Matt Johnston <matt@ucc.asn.au>
date Mon, 24 Mar 2014 20:48:28 +0800
parents 46e327c00246
children 6dd157a12035
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import time
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import fcntl
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import sys
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 class AtomicFile(object):
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 DELAY = 0.5
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 def __init__(self, name):
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 self.name = name
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 def write(self, data, timeout = 5):
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 try:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 end = time.time() + timeout
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 with open(self.name, "r+") as f:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 while timeout == 0 or time.time() < end:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 try:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 except IOError:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 time.sleep(DELAY)
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 continue
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 f.write(data)
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 return True
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 except IOError, e:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 print>>sys.stderr, e
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 return False
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 def read(self, timeout = 5):
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 try:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 end = time.time() + timeout
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 with open(self.name, "r") as f:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 while timeout == 0 or time.time() < end:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 try:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 except IOError:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 time.sleep(DELAY)
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 continue
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 return f.read()
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 except IOError, e:
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 print>>sys.stderr, e
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return None