in reply to Variables loop in Parallel::ForkManager
Without forking, it would be
open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { chomp( my $linex = $_ ); open my $fh_y, '<', 'y' or die $!; open my $fh_out, '>', $linex or die $!; while (<$fh_y>) { chomp( my $liney = $_ ); print $fh_out ("$linex$liney\n"); } }
So with forking, it would be
open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { $pm->start and next; chomp( my $linex = $_ ); open my $fh_y, '<', 'y' or die $!; open my $fh_out, '>', $linex or die $!; while (<$fh_y>) { chomp( my $liney = $_ ); print $fh_out ("$linex$liney\n"); } $pm->finish }
Since every thread reads file y, you could preload it into memory to save the threads from all doing the same work.
Without forking, it would be
my @y; { open my $fh_y, '<', 'y' or die $!; chomp( @y = <$fh_y> ); } open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { chomp( my $linex = $_ ); open my $fh_out, '>', $linex or die $!; for my $liney (@y) { print $fh_out ("$linex$liney\n"); } }
So with forking, it would be
my @y; { open my $fh_y, '<', 'y' or die $!; chomp( @y = <$fh_y> ); } open(my $fh_x, '<', 'x') or die $!; while (<$fh_x>) { $pm->start and next; chomp( my $linex = $_ ); open my $fh_out, '>', $linex or die $!; for my $liney (@y) { print $fh_out ("$linex$liney\n"); } $pm->finish }
Update: Added preloading version.
Update: Fixed bug mentioned in reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Variables loop in Parallel::ForkManager
by jackyex (Initiate) on Nov 29, 2008 at 22:27 UTC | |
by ikegami (Patriarch) on Nov 29, 2008 at 23:50 UTC | |
by jackyex (Initiate) on Nov 30, 2008 at 08:22 UTC | |
by ig (Vicar) on Nov 30, 2008 at 09:29 UTC | |
by ikegami (Patriarch) on Nov 30, 2008 at 15:54 UTC |