[Python-Dev] best place for an atomic file API

Charles-François Natali neologix at free.fr
Wed Feb 15 22:16:13 CET 2012


Hi,

Issue #8604 aims at adding an atomic file API to make it easier to
create/update files atomically, using rename() on POSIX systems and
MoveFileEx() on Windows (which are now available through
os.replace()). It would also use fsync() on POSIX to make sure data is
committed to disk.
For example, it could be used by importlib to avoid races when
writting bytecode files (issues #13392, #13003, #13146), or more
generally by any application that wants to make sure to end up with a
consistent file even in face of crash (e.g. it seems that mercurial
implemented their own version).

Basically the usage would be, e.g.:

with AtomicFile('foo') as f:
    pickle.dump(obj, f)

or

with AtomicFile('foo') as f:
   chunk = heavyCrunch()
   f.write(chunk)
   chunk = CrunchSomeMore()
   f.write(chunk)


What would be the best place for a such a class?
_pyio, tempfile, or a new atomicfile

Cheers,

cf


More information about the Python-Dev mailing list