Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Timeouts: Any alternative to alarm in Win32?

by BrowserUk (Patriarch)
on Dec 04, 2008 at 01:22 UTC ( [id://727846]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Timeouts: Any alternative to alarm in Win32?
in thread Timeouts: Any alternative to alarm in Win32?

Is there any advantage ...

That is both easy and difficult to give a straight answer to.

  1. I've used variations of that successfully.
  2. I've tried using IO::Select with blocking sockets and had mixed success.

    can_read() sometimes (often?) returns true when nothing is available.

    Mixing select with buffered IO seems to give unreliable results. Which might be explained by the notes and warning in perlfunc (or not?).

    Note: on some Unixes, the select(2) system call may report a socket file descriptor as "ready for reading", when actually no data is available, thus a subsequent read blocks. It can be avoided using always the O_NONBLOCK flag on the socket. See select(2) and fcntl(2) for further details.

    WARNING: One should not attempt to mix buffered I/O (like read or <FH>) with select, except as permitted by POSIX, and even then only on POSIX systems. You have to use sysread instead.

    With non-blocking IO, false can_reads are nothing more than an annoyance. With blocking IO, they can be a infinite wait. (Or as it was called in my Mech.Eng days, a 'long wait', which is exactly what the unsuspecting apprentice experienced when he was sent to the stores to get one :)

    Mixing non-blocking and blocking IO is useful. Sometimes you don't want to wait more than a few seconds to discover that the server isn't going to respond, but once it has responded, you're prepared to wait whilst it makes up its mind what it will say next. Using a timed initial read saves waiting forever, but falling back onto the built-in blocking IO and all its conveniences, is useful once the conversation is in progress.

    I simply don't know if IO::Select suffers the same limitations as select, but I suspect that it probaly does.

Essentially, having seen can_read() return true when nothing was available, and had the code enter an infinite read state on several occasions (generally when my ISP decided to timeout my connnection), I'm very shy of using select or IO::Select in conjuction with blocking IO (the diamond operator and friends). Maybe I'm talking out of my a***; maybe I've just been 'unlucky'; but that's my experience.

And you know, one of the definitions of madness is doing the same thing over and over and expecting the outcome to be different. Since I do not consider myself to be mad (that's another strong indicator :), when I get bitten a few times, I tend to get shy.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Timeouts: Any alternative to alarm in Win32?
by ikegami (Patriarch) on Dec 04, 2008 at 12:27 UTC

    can_read() sometimes (often?) returns true when nothing is available.

    Well that's problematic! I know it returns true when the handle has been closed. That's a good thing. sysread returns zero allowing you to handle disconnects. But I presume that's not what you mean.

    I simply don't know if IO::Select suffers the same limitations as select, but I suspect that it probaly does.

    IO::Select is just a thin wrapper for select that handles packing and unpacking the bit vectors for you. If it affects select, it affects IO::Select.

    Mixing select with buffered IO seems to give unreliable results.

    As documented. select translates into a system call and pays no attention to Perl's buffers.

    I'm very shy of using select or IO::Select in conjuction with blocking IO (the diamond operator and friends).

    As you should be. The diamond operator does buffered I/O which is incompatible with select.

    when I get bitten a few times, I tend to get shy.

    It wasn't a trick question. I have little real-world experience in the matter. Thanks for the info.

Re^4: Timeouts: Any alternative to alarm in Win32?
by Anonymous Monk on Sep 01, 2009 at 10:34 UTC

    Comment from Net::Wire10 source code:

    # If select() said data is available, but it # was not, it means the connection was lost.

    I interpret this to be that IO::Select on a blocking socket will falsely return "data available" if the OS TCP socket is in one of the disconnected states.

    Another comment:

    # IO::Select sometimes returns undef # instead of an empty array.

    So beware of that..

    And a third one about alarm() and can_read():

    Alarms are silently cancelled after IO::Select->can_read() is called.

    Of course, $SIG{ALRM} and signals in general is usually busted in many other ways, so you probably avoid a lot of trouble by steering clear of signals completely, making this less of an issue. Net::Wire10 uses threads::shared and a shared instance variable for signaling.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found