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

for this part of code is given down and it is working fine. This just gives the idea of how output of different process is handled in temprary files.
A number of subprocessed (more than three at least) are forked and these subprocess logs their output in temporary files. as a processes completes its execution its output from its temporary file is displayed on the consol. the code for output handling is given below (It does not include the code for forking process, autofleshing, reading from childs IO::Select and other stuff.) it works fine.
my question is: can i display the output of one process in real time instead of loging it in temporary file and the output of other the remaining processes should be loged in temporary files simultaneosuly. The peice of code
------------------ sub Pooled_function { my($id,$parentfh) = @_; print $parentfh "start\n"; close(STDOUT); close(STDERR); while (1) { chomp(my $pclass = <$parentfh>); my $file = "$build_logs_dir/$pclass.log"; open(STDOUT, "> $file") or die "Can't redirect stdout: to $file " +; open(STDERR, ">&STDOUT"); # ----------- Do the stuff ------ close(STDOUT) or die "Can't close STDOUT: $!"; close(STDERR) or die "Can't close STDERR: $!"; print $parentfh "$id $pclass $result\n"; } close($parentfh); }

Replies are listed 'Best First'.
Re: output of multiple processes
by moritz (Cardinal) on May 26, 2008 at 09:37 UTC
Re: output of multiple processes
by pc88mxer (Vicar) on May 26, 2008 at 16:19 UTC

    From your code it looks like:

    • Pooled_function is being executed in the child process
    • the value of $pclass read from $parentfh indicates what work the child process should perform in addition to what file the output should be saved in
    Are these correct assumptions?

    If so, you can use the tee command, but it requires juggling some file handles:

    sub Pooled_function { open(my $STDOUT_SAVE, ">&STDOUT"); # save current STDOUT while (<$parentfh>) { chomp; my ($pclass, $copy_to_console) = split(' ', $_, 2); my $file = ...; if ($copy_to_console) { open(STDOUT, ">&", $STDOUT_SAVE); open(OUT, "|-", "tee", $file); } else { open(OUT, ">", $file); } open(STDOUT, ">&OUT"); open(STDERR, ">&OUT"); # Do the stuff close(STDOUT); close(STDERR); close(OUT); } close($parentfh); }
    Alternatively, in the Perl Cookbook, there is an example of how to implement tee within perl by using tied file handles. Look for the Tie::Tee example at the end of Chapter 13.