http://www.perlmonks.org?node_id=894754

Lightknight has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a module, Parallel::Loops, that now uses IO::Select on a pipe's handles. However select only works on windows for sockets. And perlport says that:
       select  Only implemented on sockets. (Win32, VMS)
               Only reliable on sockets. (RISC OS)
I guess I could test for Win32 || VMS || RISC_OS somehow (by finding what $^O is on all those platforms?). But that is brittle. Is there "a better way" to judge whether select works without having to fork and without it costing (too much) runtime? ($Config{d_select} eq 'define') seems to be true for both linux and Windows, unhelpfully.

Replies are listed 'Best First'.
Re: How to test for reliable select()?
by BrowserUk (Patriarch) on Mar 22, 2011 at 10:08 UTC
    Is there "a better way" to judge whether select works without having to fork and without it costing (too much) runtime?

    This is as much a question as a suggestion.

    On an OS where select does work on pipes, if you create a pipe, write to the write handle and then immediately select on the read handle with a timeout, shouldn't you get the read handle's fileno returned immediately? On Windows, you get 0:

    pipe $in, $out;; print $out 'fred';; $si = ''; vec $si, fileno( $in ), 1;; print select $si, undef, undef, 0.1;; 0 0.1

    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.
Re: How to test for reliable select()?
by ambrus (Abbot) on Mar 22, 2011 at 11:22 UTC

    You might also try to use a proper portable event module like EV which will do as much as it can on many platforms including Win32.

Re: How to test for reliable select()?
by ikegami (Patriarch) on Mar 22, 2011 at 17:20 UTC

    Sockets can be used instead of pipes. The following works on Windows (and on unix, of course):

    use Socket qw( AF_UNIX SOCK_STREAM PF_UNSPEC ); sub _pipe { socketpair($_[0], $_[1], AF_UNIX, SOCK_STREAM, PF_UNSPEC) or return undef; shutdown($_[0], 1); # No more writing for reader shutdown($_[1], 0); # No more reading for writer return 1; }

    (From Re^4: why IPC::Open3 can't execute MS-DOS "dir" command?)