나는 약간의 bash 스크립트를 사용하고 있지만, 다운로드 속도, ETA 및 이와 유사한 정보를 추출하는 데 몇 시간이 걸릴 수 있습니다. 이 정보를 펄에서 포착 할 필요가 있지만 문제가 생기고 출력물을 라인별로 읽을 수 없다.Perl에서 외부 명령의 출력을 실시간으로 어떻게 읽을 수 있습니까?
어떤 도움이 필요합니까?
EDIT :이 부분을 좀더 자세히 설명하기 위해 나는 서로 나란히있는 여러 bash 스크립트를 실행하고 있는데 gtk를 사용하여 gtk를 사용하여 편리한 진행률 막대를 생성하고자합니다. 현재 실행하고자하는 모든 bash 스크립트에 대해 2 개의 스레드를 실행 중입니다. 그래픽 정보를 업데이트하기위한 하나의 마스터 스레드입니다. 그것은 다음과 같이 보입니다 (가능한 한 많이 줄일 수 있습니다) :
my $command1 = threads->create(\&runCmd, './bash1', \@out1);
my $controll1 = threads->create(\&monitor, $command1, \@out1);
my $command1 = threads->create(\&runCmd, 'bash2', \@out2);
my $controll2 = threads->create(\&monitor, $command2, \@out2);
sub runCmd{
my $cmd = shift;
my @bso = shift;
@bso = `$cmd`
}
sub monitor{
my $thrd = shift;
my @bso = shift;
my $line;
while($thrd->is_running()){
while($line = shift(@bso)){
## I check the line and do things with it here
}
## update anything the script doesn't tell me here.
sleep 1;# don't cripple the system polling data.
}
## thread quit, so we remove the status bar and check if another script is in the queue, I'm omitting this here.
}
실제로 스레드 대신 POE와 같은 적절한 이벤트 루프를 사용해야합니다. 당신은 POE :: Wheel :: Run으로 훨씬 더 성공적으로 성공할 것입니다. (AnyEvent :: Subprocess를 권하고 싶지만, 주요한 리펙토링을 겪고 있으며 즉시 문제를 해결하지는 못할 것입니다.) – jrockway