pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:
Hail all Monks!
I want to try reading from a handle (a pipe in my case) and timing out if nothing happens after X seconds. I'm using a $SIG{ALRM} right now, but that involves a callback when all I really want to do is stop trying to read and continue execution right after the read. I found myself considering a GOTO inside the $SIG{ALRM} callback, and that's when I decided a perlmonks post was in order.
To clarify, here's the code of my $SIG{ALRM} rendition:
# $read is an open pipe from earlier local $SIG{ALRM} = sub { goto('WIBBLE'); }; alarm(60); $result = <$read>; :WIBBLE if ( !defined($result) ) { print "Oh well, I didn't need that anyway...\n"; }
And to restate my question: What's the best way to handle a timeout like this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Best way to timeout on a read
by moritz (Cardinal) on Aug 20, 2008 at 18:11 UTC | |
by pjotrik (Friar) on Aug 20, 2008 at 18:46 UTC | |
|
Re: Best way to timeout on a read
by Perlbotics (Archbishop) on Aug 20, 2008 at 18:16 UTC |