Atomic file write operations (cross platform)

后端 未结 7 1802
礼貌的吻别
礼貌的吻别 2020-12-28 15:34

How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.
For the record, reads are far greater than writ

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 15:58

    In Linux, Solaris, Unix this is easy. Just use rename() from your program or mv. The files need to be on the same filesystem.

    On Windows, this is possible if you can control both programs. LockFileEx. For reads, open a shared lock on the lockfile. For writes, open an exclusive lock on the lockfile. Locking is weird in Windows, so I recommend using a separate lock file for this.

提交回复
热议问题