Subject: | How to get atomic writes with Log::Dispatch::File |
It seems that writes done from multiple processes to a shared file are only atomic if the file was opened with O_APPEND, at least on Linux and for reasonable string lengths. The attached script seems to confirm this statement --- with '>>' I get always 1000 lines of output, with '>' it's always less than 1000.
It would be nice if this could be mentioned in the Log::Dispatch::File documentation.
Some pointers about atomic writes:
* http://stackoverflow.com/questions/1154446/is-file-append-atomic-in-unix/
* http://article.gmane.org/gmane.linux.kernel/43445
* http://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/
Subject: | parallel_log_dispatch.pl |
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Parallel::ForkManager;
use Log::Dispatch;
my $pm = Parallel::ForkManager->new(24);
truncate '/tmp/test.out', 0;
my $log = Log::Dispatch->new(
outputs => [
[
'File',
min_level => 'info',
filename => '/tmp/test.out',
mode => '>', # try '>' and '>>'
newline => 1
]
],
);
for (1..1000) {
$pm->start and next;
$log->emerg("no " . sprintf("%04d", $_) . ("x"x1024));
$pm->finish;
}
$pm->wait_all_children;
__END__
=pod
Check with
wc /tmp/test.out
Output should be
1000 2000 1032000 /tmp/test.out
=cut