AnyEvent::FileLock - Lock files asynchronously
use AnyEvent::FileLock; my $w = AnyEvent::FileLock->flock(file => $fn, cb => sub { ... }, mode => '<', delay => $seconds, timeout => $timeout);
This module tries to lock some file repeatly until it success or a timeout happens.
The function provides a unique method flock accepting the following arguments:
flock
When this argument is given the passed file handle is used as the file locking target.
When this argument is given a file with the given name will be opened or created and then the module will try to lock it.
$lock_type may be fcntl or flock and determines the way used to obtain the lock under the hood.
$lock_type
fcntl
The default is flock.
In order to use locks of type fcntl, the module Fctnl::Packer has to be also instaled.
The given function is called once the lock is acquired on the file with the file handle passed as an argument.
In case of error (i.e. timeout) undef will be passed instead of the file handle. The error can be retrieved from $!.
undef
$!
The user is responsible for closing the file handle or calling flock($fh, LOCK_UN) (or the corresponding fcntl(2) F_UNLCK) on it when required.
flock($fh, LOCK_UN)
The mode used to open the file when the argument file is passed. Accepted values are <, >, >>, +< and +>.
file
<
>
>>
+<
+>
The mode used when locking the file, it accepts the same set of values as open_mode. < means shared access and everything else exclusive access.
open_mode
Configures both open_mode and lock_mode.
lock_mode
The operation is aborted if the lock operation can not be completed for the given lapse.
Note that this timeout is aproximate, it is checked just after every failed locking attempt.
Time to be delayed between consecutive locking attemps. Defaults to 1 second.
When fcntl is selected as the lock type, this three additional arguments can be used to define the section of the file that has to be locked. See fcntl(2) for the details.
AnyEvent.
"flock" in perlfunc, "fcntl" in perlfunc, fcntl(2), Fcntl, Fcntl::Packer.
Salvador Fandiño, <sfandino@yahoo.com>
Copyright (C) 2013 by Qindel Formaciñn y Servicios S.L.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install AnyEvent::FileLock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::FileLock
CPAN shell
perl -MCPAN -e shell install AnyEvent::FileLock
For more information on module installation, please visit the detailed CPAN module installation guide.