annotate web/atomicfile.py @ 534:0cc7f2dda0e8

missing sensor.py
author Matt Johnston <matt@ucc.asn.au>
date Tue, 05 May 2015 21:29:19 +0800
parents 236e5d131b3e
children 87c20b8c5472
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):
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
7 DELAY = 0.5
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
8 def __init__(self, name):
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
9 self.name = name
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
11 def write(self, data, timeout = 5):
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
12 try:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
13 end = time.time() + timeout
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
14 with open(self.name, "r+") as f:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
15 while timeout == 0 or time.time() < end:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
16 try:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
17 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
18 except IOError:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
19 time.sleep(DELAY)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
20 continue
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
22 os.ftruncate(f.fileno(), 0)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
23 f.write(data)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
24 return True
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
26 except IOError, e:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
27 print>>sys.stderr, e
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
29 return False
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
31 def read(self, timeout = 5):
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
32 try:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
33 end = time.time() + timeout
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
34 with open(self.name, "r") as f:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
35 while timeout == 0 or time.time() < end:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
36 try:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
37 fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
38 except IOError:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
39 time.sleep(DELAY)
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
40 continue
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
42 return f.read()
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
44 except IOError, e:
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
45 print>>sys.stderr, e
489
46e327c00246 gets current params
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
501
236e5d131b3e Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents: 489
diff changeset
47 return None