Re: question about forked processes writing to the same file



A. Sinan Unur wrote:
it_says_BALLS_on_your forehead wrote:

use strict; use warnings; use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(10);

# assume @files contains 100 files that will be processed,
# and processing time could range from subseconds to hours

my $out = 'results.txt';
for my $file (@files) {
 $pm->start and next;

 # some code to process file
 # blah blah blah

This is a good place to put an exclusive flock on a sentinel file (not the output file). You code will block until it gets the exclusive lock.

Why not the output file?

open( my $fh_out, '>>', $out ) or die "can't open $out: $!\n";

Why not just:

    flock $fh_out, 2 or die $!;

print $fh_out "$file\n";
close $fh_out;

And, this would be the place to release that lock.

Unless you use a lexical ref to the filehandle as above, in which case you don't need to release it explicitly.


My related comment was posted at http://groups.google.com/group/comp.lang.perl.misc/msg/2ca8d6e6894030b3

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.