[Assorted-commits] SF.net SVN: assorted: [228] python-commons/trunk/src/commons/files.py
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2008-01-13 02:30:18
|
Revision: 228
http://assorted.svn.sourceforge.net/assorted/?rev=228&view=rev
Author: yangzhang
Date: 2008-01-12 18:30:11 -0800 (Sat, 12 Jan 2008)
Log Message:
-----------
added disk double buffer
Modified Paths:
--------------
python-commons/trunk/src/commons/files.py
Modified: python-commons/trunk/src/commons/files.py
===================================================================
--- python-commons/trunk/src/commons/files.py 2008-01-13 02:29:58 UTC (rev 227)
+++ python-commons/trunk/src/commons/files.py 2008-01-13 02:30:11 UTC (rev 228)
@@ -11,6 +11,8 @@
from L{invalid_filename_chars}.
"""
+from __future__ import with_statement
+
import os, re, tempfile
def soft_makedirs( path ):
@@ -76,3 +78,28 @@
pattern = invalid_filename_chars_regex
return re.sub( pattern, '_', filename )
+class disk_double_buffer( object ):
+ """
+ A simple disk double-buffer. One file is for reading, the other is for
+ writing, and a facility for swapping the two roles is provided.
+ """
+ def __init__( self, path ):
+ self.paths = [ path + '.0', path + '.1' ]
+ self.w, self.r = 0, 1
+ # initialize empty reader file
+ with file( self.paths[ self.r ], 'w' ): pass
+ self.reload_files()
+ def reload_files( self ):
+ self.writer = file( self.paths[ self.w ], 'w' )
+ self.reader = file( self.paths[ self.r ] )
+ def switch( self ):
+ self.r, self.w = self.w, self.r
+ self.close()
+ self.reload_files()
+ def write( self, x ):
+ self.writer.write( x )
+ def read( self, len = 8192 ):
+ return self.reader.read( len )
+ def close( self ):
+ self.reader.close()
+ self.writer.close()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|