I made some changes to your basic code structure and came up with something that gets me almost there. Could you take a look and let me know if you see any glaring issues.
Also I assumed the way I have it coded would enable me to get whatever was "printed" prior to killing the thread but this does not seem to be the case, any ideas on what I am doing wrong?
UPDATED:
my $command = 'diskpart'; my (@results) = _timedCommand($command, 30); my $last = pop(@results); if ($last =~ m/^!!!TIMEOUT!!!/i) { print "Bad: \n" . join(' ', @results); } else { push(@results, $last); print "Good:\n " . join(' ', @results); } sub _timedCommand { use threads; use threads::shared; my ($command, $time) = @_; my @results :shared; my $pid :shared; my $thr = async { $pid = open my $fh, "$command |" or die "$!"; @results = <$fh>; $fh->close(); }; while ($thr->is_running() and $time > 0) { sleep(1); $time--; } if ($thr->is_joinable()) { return @results; } else { $thr->detach; kill 3, $pid; push(@results, '!!!TIMEOUT!!!'); print ("\n @results \n"); return @results; } }
Thanks
In reply to Re^2: How to deal with a forked proces that is waiting for user input?
by gepapa
in thread How to deal with a forked proces that is waiting for user input?
by gepapa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |