开发者

Perl: when doing 'open(A,"proc|")' make 'close(A)' return instantly

If I do 'open(A,"proc|")', how do I make 'close(A)' return in开发者_运维知识库stantly, even if 'proc' hasn't finished writing to stdout?

"man perlfunc" tells me:

Prematurely closing the read end of a pipe (i.e. before the process writing to it at the other end has closed it) will result in a SIGPIPE being delivered to the writer. If the other end can't handle that, be sure to read all the data before closing the pipe.

but is there a workaround? Specific example:

$|=1; 
open(A,"curl -sN http://test.barrycarter.info/bc-slow-cgi.pl|"); 

while (<A>) { 
  print "THUNK: $_\n"; 
  if (/5$/) {last;} 
} 

print "LOOP EXIT\n"; 
close(A); 
print "A CLOSED\n"; 

bc-slow-cgi.pl just prints time() once per second forever: the above code prints "LOOP EXIT", but never "A CLOSED".


close on a handle created by open -| waits for the child to end. It seems to me that the child should die from a PIPE signal or error the next time it attempts to write after you call close, but you could kill the child if you don't want to wait that long.

my $pid = open(...);

while (...) {
   ...
}

kill PIPE => $pid;
close(...);

PIPE is a bit unorthodox, but it seemed appropriate here. Feel free to send TERM or whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜