michi has asked for the wisdom of the Perl Monks concerning the following question:
if i call execute() i get the output i exspected. $buf contains all the output (including the messages from the system command). Thats fine :) Now i tried this with threads.# this is my sample function i cannot modify sub doIt() { print "blabla\n"; print STDOUT "blabla2\n"; #sample_app is a small script that writes some output to STDOUT an +d STDERR system("sample_app"); } sub execute() { pipe(RDR, WTR); my $pid = fork(); if ($pid == 0) { close STDOUT; open (STDOUT, '>&WTR'); select STDOUT; $| = 1; doIt(); print "child finished\n"; exit; } else { my $buf = ""; while ($buf !~ /child finished/) { $buf .= <RDR>; } sleep 2; print "parent got:\n$buf\n"; } }
Unfortunately this doesnt do what i expected. The output of the system command is written to the shell (and not to my pipe). At the end of the execution $buf only contains "blabla" and "blabla2". Is there a way to get this working in a threaded environment?use threads; my $t = threads->new(\&execute); $t->join();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Redirecting stdout with threads
by kennethk (Abbot) on Mar 14, 2009 at 15:57 UTC | |
by michi (Novice) on Mar 15, 2009 at 14:54 UTC | |
by kennethk (Abbot) on Mar 15, 2009 at 15:37 UTC |