2011-12-17 25 views
4

我有一個應用程序更新一個CSV文件(單個),CSV從幾個進程中隨機更新,我猜如果兩個進程嘗試更新它(添加一行...),有些數據會丟失,我猜或覆蓋(?)。在幾個進程間共享一個文件[Perl]

避免這種情況的最好方法是什麼?

感謝,

+6

聽起來像你寧願想要使用某種數據庫。如果你真的想繼續使用CSV,我建議使用[flock()](http://perldoc.perl.org/functions/flock.html),但是你必須重寫你的過程才能夠等待文件可用。 – Fluff 2011-12-17 11:43:50

+0

哪個操作系統? – dolmen 2011-12-17 14:58:50

+0

linux/ubuntu操作系統 – snoofkin 2011-12-17 15:14:34

回答

6

使用Perl的DBIDBD::CSV driver訪問您的數據;那會照顧你的flock。 (除非您使用的是Windows 95或舊版Mac OS。)如果您決定稍後切換到RDBMS,則會做好充分的準備。

簡單的flock ing @Fluff建議當然也應該沒問題。

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) 
相關問題