tmalyib has asked for the wisdom of the Perl Monks concerning the following question:

Monks, are there any good solutions for file access over a slow or busy NFS link? I have tried the following but I am not sure if it is the best solution.
sub checkfile { my $file = shift; my $duration = shift || 10; eval { local $SIG{ALRM} = sub { die "file access to $file failed\n" }; alarm $duration; do { eval { die "cannot access $file\n" unless -e $file; }; sleep 1 if $@; } until !$@; alarm 0; }; return if $@; return 1 unless $@; }

Replies are listed 'Best First'.
Re: perl and slow NFS
by wazoox (Prior) on Oct 18, 2007 at 14:40 UTC
    NFS default behaviour is blocking IOs, that means the program accessing the share may eventually be frozen and can't be killed. I guess to circumvent this you need forking, accessing data from child, and waiting from father:
    sub checkfile { my $file = shift; my $duration = shift || 10; my $pid = fork(); unless ($pid) { # this is run from child die "cannot access $file\n" unless -e $file; } else { # this is run from parent sleep $duration; if ( kill 0, $pid) { warn "$pid still running after $duration seconds!"; kill 9, $pid; return; } return 1 } }