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