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
    Thank you but it doesn't work...Maybe I'm doing something wrong : #!/usr/bin/perl use Parallel::ForkManager; my $pm = new Parallel::ForkManager(3); 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 (@y) { print $fh_out ("$linex$liney\n"); } $pm->finish } When I perl script.pl it gives me the 5 "$filex" files but like this : jacky@ubuntu:~/Desktop/test$ cat 1 1 1 1 So definatelly there is something wrong in there...Thank you again
      for (@y) should be for my $liney (@y)
        Thank you,but :D it still doesn't work I did what you said but now it gives me the files like this : jacky@ubuntu:~/Desktop/test$ cat 1 1jack 1john 1joe jacky@ubuntu:~/Desktop/test$ cat 2 2jack 2john 2joe And so on...Any fix to this too? , Jack