perl - Fork in a loop. Simultaneous process -
i didn't quite understand fork function.
how print output files simultaneously, utilize separate process each file?
my $regex = $argv[0]; (@argv[1 .. $#argv]){ open (my $fh, "<", $_); foreach (<$fh>){ print "$1\n" if $_ =~ /(\b$regex\b)/; } }
presumably, want limit number of simultaneous processes. parallel::forkmanager makes easy.
use parallel::forkmanager qw( ); $pm = parallel::forkmanager->new($max_processes); $filter = shift(@argv); $filter = qr/$filter/; $qfn (@argv) { $pm->start() , next; open(my $fh, '<', $qfn) or die("can't open \"$qfn\": $!\n"); while (<$fh>) { print "$1\n" if /(\b$regex\b)/; } } $pm->finish();
if didn't want limit number of simultaneous processes, similar.
my $filter = shift(@argv); $filter = qr/$filter/; $qfn (@argv) { fork() , next; open(my $fh, '<', $qfn) or die("can't open \"$qfn\": $!\n"); while (<$fh>) { print "$1\n" if /(\b$regex\b)/; } } 1 while wait() > 0;
perl loops process
No comments:
Post a Comment