10 Aug 2021 02:34:52 UTC
- Distribution: File-Flock-Retry
- Module version: 0.632
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (2318 / 0 / 6)
- KwaliteeBus factor: 1
- 77.88% Coverage
- License: perl_5
- Perl: v5.10.1
- Activity24 month
- Download (17.01KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version++ed by:1 non-PAUSE userPERLANCAR perlancar
- SEE ALSO
- COPYRIGHT AND LICENSE
File::Flock::Retry - Yet another flock module
This document describes version 0.632 of File::Flock::Retry (from Perl distribution File-Flock-Retry), released on 2021-08-10.
use File::Flock::Retry; # try to acquire exclusive lock. if fail to acquire lock within 60s, die. my $lock = File::Flock::Retry->lock($file); # explicitly unlock $lock->release; # automatically unlock if object is DESTROY-ed. undef $lock;
This is yet another flock module. It is a more lightweight alternative to File::Flock with some other differences:
OO interface only
Autoretry (by default for 60s) when trying to acquire lock
I prefer this approach to blocking/waiting indefinitely or failing immediately.
$lock = File::Flock::Retry->lock($path, \%opts)
Attempt to acquire an exclusive lock on
$path. By default,
$pathwill be created if not already exists (see "mode"). If
$pathis already locked by another process, will retry every second for a number of seconds (by default 60). Will die if failed to acquire lock after all retries.
Will automatically unlock if
$lockgoes out of scope. Upon unlock, will remove
$pathif it is still empty (zero-sized).
Integer. Default: O_CREAT | O_RDWR.
File open mode, to be passed to Perl's
sysopen(). For example, if you want to avoid race condition between creating and locking the file, you might want to use
O_CREAT | O_EXCL | O_RDWRto fail when the file already exists. Note that the constants are available after you do a
use Fcntl ':DEFAULT';.
Integer. Default: 60.
Number of retries (equals number of seconds, since retry is done every second).
Boolean. Default: 0.
By default, an exclusive lock (LOCK_EX) is attempted. However, if this option is set to true, a shared lock (LOCK_SH) is attempted.
Unlock. will remove lock file if it is still empty.
Synonym for "unlock".
my $fh = $lock->handle;
Return the file handle.
Please visit the project's homepage at https://metacpan.org/release/File-Flock-Retry.
Source repository is at https://github.com/perlancar/perl-File-Flock-Retry.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=File-Flock-Retry
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Not yet tested on Windows. Some filesystems do not support inode?
File::Flock, a bit too heavy in terms of dependencies and startup overhead, for my taste. It depends on things like File::Slurp and Data::Structure::Util (which loads Digest::MD5, Storable, among others).
File::Flock::Tiny which is also tiny, but does not have the autoremove and autoretry capability which I want. See also: https://github.com/trinitum/perl-File-Flock-Tiny/issues/1
flock() Perl function.
An alternative to flock() is just using sysopen() with O_CREAT|O_EXCL mode to create lock files. This is supported on more filesystems (particularly network filesystems which lack flock()).
This software is copyright (c) 2021, 2019, 2017, 2015, 2014 by perlancar <email@example.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install File::Flock::Retry, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install File::Flock::Retry
For more information on module installation, please visit the detailed CPAN module installation guide.