Re: question about forked processes writing to the same file




A. Sinan Unur wrote:
> simon.chao@xxxxxxxxx wrote in
> news:1129994532.138751.160740@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
>
> > Gunnar Hjalmarsson wrote:
>
> >> I can add that I'm using the above method in a widely used Perl
> >> program (run on various platforms), without any problems having been
> >> reported. However, I do use flock() to set an exclusive lock before
> >> printing to the file. Can't tell if locking is necessary.
>
> ...
>
> > hello Gunnar, could you please post a snippet of code illustrating how
> > you would use flock() in my above example?
>
> So far,I do not see any code posted by you.
>
> Maybe, you should read the documentation, and come up with a short
> script illustrating your problem, and we can comment on it:
>
> perldoc -q lock
>
> perldoc -f flock
>

apologies, i was referring to the following example:
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


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


$pm->finish;
}
$pm->wait_all_children;

.



Relevant Pages