in reply to Fork and wait question

Just screwing around.... but I thought of using a piped open as an easy fork-an-exec. One problem, the filehandle in a hash won't print out. Anyone know why? Would this type of approach work?
#!/usr/bin/perl use warnings; use strict; my %pids; my @to_be_processed = (1..20); my $cmd = 'echo'; foreach my $p (@to_be_processed){ $pids{$p}{'pid'} = open($pids{$p}{'fh'}, "$cmd $p 2>&1 |"); } # get output dosn't work? get globs foreach my $key(keys %pids){ while (<{ $pids{$key}{'fh'} }>){print $_} } foreach my $key(keys %pids){ waitpid($pids{$key}, 1); } print "done\n";

I'm not really a human, but I play one on earth CandyGram for Mongo

Replies are listed 'Best First'.
Re^2: Fork and wait question
by samtregar (Abbot) on Jun 12, 2008 at 18:51 UTC
    The <> operator is super fussy due to being overloaded with ancient glob() craziness. It only works if you put in a simple scalar, not any kind of expression. This works:

    my $fh = $pids{$key}{'fh'}; while (<$fh>){print $_}

    This kind of thing will work fine until your sub-processes need to produce more than 4k of data (or whatever your buffer size is). Then the sub-procs will block when they print() and won't work in parallel while you're collecting results. To fix this you need to switch to using something like IO::Select or EV to pull data from each process as it's ready.

    -sam

Re^2: Fork and wait question
by pc88mxer (Vicar) on Jun 12, 2008 at 18:33 UTC
    Calling close on the file handles will also wait for the processes to complete.
      Where do you put the close for the file handlers?
        Use it instead of the waitpid call:
        foreach my $key(keys %pids){ close $pids{$key}{'fh'}; }