Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: IO::Select and alarm()

by ikegami (Pope)
on Mar 19, 2009 at 14:48 UTC ( #751741=note: print w/ replies, xml ) Need Help??

in reply to IO::Select and alarm()

use Errno qw( EINTR ); for (;;) { my @ready = $select->can_read() or do { next if $! == EINTR; last; }; for my $fh (@ready) { ... } }


Note that die("select: $!\n"); would be more appropriate than last, but I preserved the behaviour of the OP.

Comment on Re: IO::Select and alarm()
Select or Download Code
Re^2: IO::Select and alarm()
by Maddingue (Sexton) on Mar 19, 2009 at 15:17 UTC

    Thank you, it works.

    Now I feel stupid because I should have remembered this :)

      There's a difference between can_read and select. can_read returns immediately when the object has no file handles, but I assumed it sleeps indefinitely like select in that situation.

      That means there's a bug in my earlier version. The OP's code is equivalent to

      use Errno qw( EINTR ); while ($select->count()) { my @ready = $select->can_read() or do { next if $! == EINTR; last; # Should be die("select: $!\n"); }; for my $fh (@ready) { ... } }
        I just spotted this note and have a couple of questions on this myself. I have a piece of code that also uses can_read() and needs to deal with signals. Or does it?

        My code is actually modeled after the example in the IO:Select man page here - which does not do anything special about premature wakeup:

        while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } }

        Since my code DOES seems to work correctly, even when dealing with hundreds of connections while an alarm is going off every second, my question becomes WHY does it work when I'm not paying any attention to EINTR? Am I just lucky - I highly doubt it? A number of people have been using this code for years...

        When I instrumented the code it looks like the timer interrupt is indeed waking it from the can_read(). But then it immediately falls through the loop and cycles around back to the can_read(), and also wakes when there real data to process and deal with it appropriately.

        As an aside, I also did try the recommended way for doing this, which pays attention to EINTR, and that works as well. So the questions then become is one way preferred to the other and why does my code, written the way it is, seem to be rock solid?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://751741]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (17)
As of 2014-10-23 20:00 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (128 votes), past polls