Yes you can. A value that can be passed to flock causes it not to pause. (It is called a nonblocking flock.) To find out whether a flock would pause, put a |LOCK_NB after the lock type like this:
use Fcntl qw(:flock);
# Attempt to get an exclusive lock, but don't wait for it.
if (not flock( LF, LOCK_EX|LOCK_NB )) {
print "Could not get the lock: $!";
}
You can even wait for a lock for a while, and then print a message if you don't get it eventually:
use Fcntl qw(:flock);
$lock_attempts = 3;
while (not flock( LF, LOCK_EX|LOCK_NB )) {
sleep 5; # Wait 5 seconds
$lock_attempts--; # Count down chances...
die "Could not get lock!" if (not $attempts);
}
|