Mercurial > templog
annotate web/atomicfile.py @ 228:d9e81a563923
porting to asyncio
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 20 Mar 2015 20:12:25 +0800 |
parents | 6dd157a12035 |
children | 87c20b8c5472 |
rev | line source |
---|---|
191 | 1 import os |
2 import time | |
3 import fcntl | |
4 import sys | |
5 | |
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 | 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 | 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 | 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 | 28 |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
191
diff
changeset
|
29 return False |
191 | 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 | 41 |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
191
diff
changeset
|
42 return f.read() |
191 | 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 | 46 |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
191
diff
changeset
|
47 return None |