[Spambayes-checkins] SF.net SVN: spambayes:[3180] trunk/spambayes/spambayes/safepickle.py

montanaro at users.sourceforge.net montanaro at users.sourceforge.net
Tue Nov 25 02:34:26 CET 2008


Revision: 3180
          http://spambayes.svn.sourceforge.net/spambayes/?rev=3180&view=rev
Author:   montanaro
Date:     2008-11-25 01:34:26 +0000 (Tue, 25 Nov 2008)

Log Message:
-----------
new module implementing locking pickle reads and writes.

Added Paths:
-----------
    trunk/spambayes/spambayes/safepickle.py

Added: trunk/spambayes/spambayes/safepickle.py
===================================================================
--- trunk/spambayes/spambayes/safepickle.py	                        (rev 0)
+++ trunk/spambayes/spambayes/safepickle.py	2008-11-25 01:34:26 UTC (rev 3180)
@@ -0,0 +1,55 @@
+"""Lock pickle files for reading and writing."""
+
+import sys
+import os
+import cPickle as pickle
+
+import lockfile
+
+from spambayes.Options import options
+
+def pickle_read(filename):
+    """Read pickle file contents with a lock."""
+    lock = lockfile.FileLock(filename)
+    lock.acquire(timeout=20)
+    try:
+        return pickle.load(open(filename, 'rb'))
+    finally:
+        lock.release()
+
+def pickle_write(filename, value, protocol=0):
+    '''Store value as a pickle without creating corruption'''
+
+    lock = lockfile.FileLock(filename)
+    lock.acquire(timeout=20)
+
+    try:
+        # Be as defensive as possible.  Always keep a safe copy.
+        tmp = filename + '.tmp'
+        fp = None
+        try: 
+            fp = open(tmp, 'wb') 
+            pickle.dump(value, fp, protocol) 
+            fp.close() 
+        except IOError, e: 
+            if options["globals", "verbose"]: 
+                print >> sys.stderr, 'Failed update: ' + str(e)
+            if fp is not None: 
+                os.remove(tmp) 
+            raise
+        try:
+            # With *nix we can just rename, and (as long as permissions
+            # are correct) the old file will vanish.  With win32, this
+            # won't work - the Python help says that there may not be
+            # a way to do an atomic replace, so we rename the old one,
+            # put the new one there, and then delete the old one.  If
+            # something goes wrong, there is at least a copy of the old
+            # one.
+            os.rename(tmp, filename)
+        except OSError:
+            os.rename(filename, filename + '.bak')
+            os.rename(tmp, filename)
+            os.remove(filename + '.bak')
+    finally:
+        lock.release()
+


Property changes on: trunk/spambayes/spambayes/safepickle.py
___________________________________________________________________
Added: svn:eol-style
   + native


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the Spambayes-checkins mailing list