On 2024-07-07 23:27, Barry via Python-list wrote:


On 7 Jul 2024, at 22:13, Chris Green via Python-list <python-list@python.org> 
wrote:

a simple file lock can then
be used to prevent simultaneous access (well, simultaneous access when
the writing process is writing).

There is a simple pattern to make this robust.

Write new values to a tmp file.
Close the tmp file.
Then use os.rename(tmpfile, productionfile).

This is guaranteed that any process that reads the file will only see all the 
old file contents or all the new file contents,  never a mix of both.

For clarity I'd recommend os.replace instead. This is because on Windows os.rename it would complain if the target file already exists, but os.replace has the same behaviour on both Linux and Windows.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to