README.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ===================
  2. python-atomicwrites
  3. ===================
  4. .. image:: https://travis-ci.com/untitaker/python-atomicwrites.svg?branch=master
  5. :target: https://travis-ci.com/untitaker/python-atomicwrites
  6. .. image:: https://ci.appveyor.com/api/projects/status/vadc4le3c27to59x/branch/master?svg=true
  7. :target: https://ci.appveyor.com/project/untitaker/python-atomicwrites/branch/master
  8. .. image:: https://readthedocs.org/projects/python-atomicwrites/badge/?version=latest
  9. :target: https://python-atomicwrites.readthedocs.io/en/latest/?badge=latest
  10. :alt: Documentation Status
  11. **Atomic file writes.**
  12. .. code-block:: python
  13. from atomicwrites import atomic_write
  14. with atomic_write('foo.txt', overwrite=True) as f:
  15. f.write('Hello world.')
  16. # "foo.txt" doesn't exist yet.
  17. # Now it does.
  18. See `API documentation <https://python-atomicwrites.readthedocs.io/en/latest/#api>`_ for more
  19. low-level interfaces.
  20. Features that distinguish it from other similar libraries (see `Alternatives and Credit`_):
  21. - Race-free assertion that the target file doesn't yet exist. This can be
  22. controlled with the ``overwrite`` parameter.
  23. - Windows support, although not well-tested. The MSDN resources are not very
  24. explicit about which operations are atomic. I'm basing my assumptions off `a
  25. comment
  26. <https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/449bb49d-8acc-48dc-a46f-0760ceddbfc3/movefileexmovefilereplaceexisting-ntfs-same-volume-atomic?forum=windowssdk#a239bc26-eaf0-4920-9f21-440bd2be9cc8>`_
  27. by `Doug Cook
  28. <https://social.msdn.microsoft.com/Profile/doug%20e.%20cook>`_, who appears
  29. to be a Microsoft employee:
  30. Question: Is MoveFileEx atomic if the existing and new
  31. files are both on the same drive?
  32. The simple answer is "usually, but in some cases it will silently fall-back
  33. to a non-atomic method, so don't count on it".
  34. The implementation of MoveFileEx looks something like this: [...]
  35. The problem is if the rename fails, you might end up with a CopyFile, which
  36. is definitely not atomic.
  37. If you really need atomic-or-nothing, you can try calling
  38. NtSetInformationFile, which is unsupported but is much more likely to be
  39. atomic.
  40. - Simple high-level API that wraps a very flexible class-based API.
  41. - Consistent error handling across platforms.
  42. How it works
  43. ============
  44. It uses a temporary file in the same directory as the given path. This ensures
  45. that the temporary file resides on the same filesystem.
  46. The temporary file will then be atomically moved to the target location: On
  47. POSIX, it will use ``rename`` if files should be overwritten, otherwise a
  48. combination of ``link`` and ``unlink``. On Windows, it uses MoveFileEx_ through
  49. stdlib's ``ctypes`` with the appropriate flags.
  50. Note that with ``link`` and ``unlink``, there's a timewindow where the file
  51. might be available under two entries in the filesystem: The name of the
  52. temporary file, and the name of the target file.
  53. Also note that the permissions of the target file may change this way. In some
  54. situations a ``chmod`` can be issued without any concurrency problems, but
  55. since that is not always the case, this library doesn't do it by itself.
  56. .. _MoveFileEx: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx
  57. fsync
  58. -----
  59. On POSIX, ``fsync`` is invoked on the temporary file after it is written (to
  60. flush file content and metadata), and on the parent directory after the file is
  61. moved (to flush filename).
  62. ``fsync`` does not take care of disks' internal buffers, but there don't seem
  63. to be any standard POSIX APIs for that. On OS X, ``fcntl`` is used with
  64. ``F_FULLFSYNC`` instead of ``fsync`` for that reason.
  65. On Windows, `_commit <https://msdn.microsoft.com/en-us/library/17618685.aspx>`_
  66. is used, but there are no guarantees about disk internal buffers.
  67. Alternatives and Credit
  68. =======================
  69. Atomicwrites is directly inspired by the following libraries (and shares a
  70. minimal amount of code):
  71. - The Trac project's `utility functions
  72. <http://www.edgewall.org/docs/tags-trac-0.11.7/epydoc/trac.util-pysrc.html>`_,
  73. also used in `Werkzeug <http://werkzeug.pocoo.org/>`_ and
  74. `mitsuhiko/python-atomicfile
  75. <https://github.com/mitsuhiko/python-atomicfile>`_. The idea to use
  76. ``ctypes`` instead of ``PyWin32`` originated there.
  77. - `abarnert/fatomic <https://github.com/abarnert/fatomic>`_. Windows support
  78. (based on ``PyWin32``) was originally taken from there.
  79. Other alternatives to atomicwrites include:
  80. - `sashka/atomicfile <https://github.com/sashka/atomicfile>`_. Originally I
  81. considered using that, but at the time it was lacking a lot of features I
  82. needed (Windows support, overwrite-parameter, overriding behavior through
  83. subclassing).
  84. - The `Boltons library collection <https://github.com/mahmoud/boltons>`_
  85. features a class for atomic file writes, which seems to have a very similar
  86. ``overwrite`` parameter. It is lacking Windows support though.
  87. License
  88. =======
  89. Licensed under the MIT, see ``LICENSE``.