Capt_Howdey has asked for the wisdom of the Perl Monks concerning the following question:

Sorry to post this again, but is seems that once the thread is a day or two old, no one looks at it and I still don't have an answer... I am submitting multiple commands in parallel using Run_parallel using a Tk GUI. The commands that I am running can take up to 5 minutes to complete (or time out). I am submitting these in the background on a UNIX system (&). With the current subroutine, If the commands are submitted one at a time, STDERR comes back fine. However, if I submit several at once, I only get STDERR back for the LAST command to complete. How can I get STDERR back for ALL commands? I cannot use any CPAN modules.
sub run_parallel { my $job_count = shift; unless (0 < $job_count) { confess("run_parallel called without a positive parallel job count +!"); } my @to_start = @_; my %running; my %errors; my $is_running = 0; while (@to_start or %running) { if (@to_start and ($is_running < $job_count)) { # Launch a job my $job = shift @to_start; unless (ref($job)) { $job = [$job]; } open (NULL, $null_file) or confess("Cannot read from $null_file: +$!"); my $proc_id = open3("<&NULL", \*CMD_OUT, \*CMD_ERR, @$job); $running{$proc_id} = $job; ++$is_running; } else { # collect a job my $proc_id = wait(); if (! exists $running{$proc_id}) { confess("Reaped unknown process $proc_id!"); } elsif ($?) { # Oops my $job = $running{$proc_id}; my ($cmd, @args) = @$job; my $err = "Running '$cmd' gave return code '$?'"; if (@args) { $err .= join "\n\t", "\nAdditional args:", @args; } $errors{$proc_id} = $err; } delete $running{$proc_id}; --$is_running; } } return %errors; $mw_status_window->fileevent(\*CMD_OUT, 'readable', \&read_out); $mw_status_window->fileevent(\*CMD_ERR, 'readable', \&read_err); } sub read_out { my $line = <CMD_OUT>; unless ($line) { close(CMD_OUT); } else { print "green", "$line\n"; } } sub read_err { my $line = <CMD_ERR>; unless ($line) { close(CMD_ERR); } else { print "red", "$line\n"; } }

Replies are listed 'Best First'.
Re: Gathering STDERR from multiple commands....
by impossiblerobot (Deacon) on Apr 02, 2002 at 17:00 UTC
    I looked at your previous post, but you didn't mention why you were unable to use any CPAN modules for this project.

    Since that is rarely the case, understanding your reasons might help someone help you.

    Update: One way of "using" a module would be to read the source to see how the module author accomplished the task, or (depending on the license) copying the entire module source into your own program.

    (I am constantly amazed at policies that consider untested "roll-your-own" solutions to be more secure than solutions that have experienced at least some level of peer review. And I'm sorry I couldn't do more to address your problem directly.)

    Impossible Robot
Re: Gathering STDERR from multiple commands....
by Capt_Howdey (Initiate) on Apr 03, 2002 at 13:21 UTC
    I work as a gov't contractor at a secure site. We are unable to install ANY outside software....