在多个进程之间共享文件 [Perl]

4

我有一个应用程序,它更新一个CSV文件(一个),CSV文件随机从几个进程中更新,如果两个进程在同一时间尝试更新它(添加一行...),我猜测会丢失一些数据,或者被覆盖。

有什么最好的方法来避免这种情况发生?

谢谢。


6
听起来你更倾向于使用某种数据库。如果你真的想继续使用CSV,我建议使用flock(),但你需要重写你的进程以等待文件可用。 - Fluff
2个回答

6
使用Perl的DBIDBD::CSV驱动程序来访问您的数据; 这将为您处理flock。 (除非您正在使用Windows 95或旧版Mac OS。)如果您决定以后切换到关系数据库管理系统,则会做好充分准备。
当然,如@Fluff所建议的那样,简单的flock也应该可以。

0
如果你想要一个简单且手动的方式来处理文件锁定。
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接