我有一个应用程序,它更新一个CSV文件(一个),CSV文件随机从几个进程中更新,如果两个进程在同一时间尝试更新它(添加一行...),我猜测会丢失一些数据,或者被覆盖。
有什么最好的方法来避免这种情况发生?
谢谢。
flock
。 (除非您正在使用Windows 95或旧版Mac OS。)如果您决定以后切换到关系数据库管理系统,则会做好充分准备。flock
也应该可以。1) As soon as a process opens the csv, it creates a lock.
(Lock can be in the form of creating a dummy file. The process has to delete
the file(lock) as soon as it is done reading/updating the csv)
2) Have each process check for file lock before trying to update the csv.
(If dummy file is present, some process is accessing the csv,
else it can update the csv)