[表示 : 全て 最新50 1-99 101- 201- 301- 401- 501- 601- 701- 801- 901- 2chのread.cgiへ]
Update time : 05/10 00:43 / Filesize : 262 KB / Number-of Response : 974
[このスレッドの書き込みを削除する]
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧] [類似スレッド一覧]


↑キャッシュ検索、類似スレ動作を修正しました、ご迷惑をお掛けしました

【Perl】ファイルロック(排他処理)について語ろう



32 名前:nobodyさん mailto:sage [02/06/24 13:52 ID:???]
Perlメモのロック処理をクラスで包んでみた。
特徴
・むちゃくちゃ遅い
・アンロックし忘れがない
・処理全体をロックする場合に便利
・どの環境でも使用できる

#Lock.pm
package Lock;
use strict;
use File::Spec;

sub new{
my($class, %opt) = @_;
my %lfh = (
dir => ($opt{lockdir} || $opt{dir} || 'lock'),
file => ($opt{lockfile} || $opt{file} || 'lockfile'),
timeout => ($opt{timeout} || 60),
trytime => ($opt{trytime} || 10),
);
$lfh{path} = File::Spec->catfile($lfh{dir}, $lfh{file});
for(my $i = 0; $i < $lfh{trytime}; $i++, sleep(1))
{
return bless \%lfh => $class if(rename($lfh{path}, $lfh{current} = $lfh{path} . time));
}
local *LOCKDIR;
opendir(LOCKDIR, $lfh{dir}) or return undef;
while(my $file = readdir(LOCKDIR)){
if ($file =~ /^$lfh{file}(\d+)/){
return bless \%lfh => $class if (time - $1 > $lfh{timeout}
and rename(File::Spec->catfile($lfh{dir}, $file) => $lfh{current} = $lfh{path} . time));
last;
}
}
closedir LOCKDIR;
return undef;
}
sub DESTROY{ rename $_[0]->{current} => $_[0]->{path};}
1;
__END__






[ 続きを読む ] / [ 携帯版 ]

全部読む 次100 最新50 [ このスレをブックマーク! 携帯に送る ] 2chのread.cgiへ
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧]( ´∀`)<262KB

read.cgi ver5.27 [feat.BBS2 +1.6] / e.0.2 (02/09/03) / eucaly.net products.
担当:undef