Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

IO::Select: select vs. can_read

by Coruscate (Sexton)
on May 05, 2003 at 08:09 UTC ( [id://255590]=perlquestion: print w/replies, xml ) Need Help??

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

I came across an old(er) snippet of code I used a while back for a tcp server with IO::Socket and IO::Select. It used the IO::Select::select() method to get a list of clients that could be read from. I swear I've always used can_read() instead, but apparently not! So my question is quite very simple: do the two snippets below do the exact same thing? If so, great, I think I'll stick with can_read. If they differ in any manner, does anyhow have the knowledge to share with me the difference(s) between the two methods?

I did a quick perldoc -m IO::Select and found the can_read() sub. It looks quite the same (ie: it utilizes select() in the same way), but with all the vector bits tech stuff thrown in, I can't tell exactly what's going on.

# Snippet One: select() while (my ($read) = IO::Select::select($clients, undef, undef, undef) ) { for my $client (@$read) { # handle this } } # Snippet Two: can_read() while (my @read = $clients->can_read()) { for my $client (@read) { # handle this } }


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re: IO::Select: select vs. can_read
by edan (Curate) on May 05, 2003 at 10:53 UTC
    Coruscate,

    The IO::Select module is really just an OO wrapper for the select builtin function, so you don't have to fiddle with that 'vector bits tech stuff'. It seems to me that if you are already going to the trouble to use the module, you might as well use the convenience functions (can_read(), can_write() etc.) provided by the module, but that's just my opinion...

    In summation, I believe your snippets will do the same thing, which is to ask select to return handles that can read...

    --
    3dan

Re: IO::Select: select vs. can_read
by MarkM (Curate) on May 05, 2003 at 13:35 UTC

    In theory, can_read() is more efficient in cases where you have a large number of clients that remains mostly constant between calls to can_read(), since add() and remove() should be performing only bit operations, wheras IO::Select::select() re-creates the bit mask every time.

    In practice, I wouldn't be able to guess which one is faster. Graham Barr (the author of IO::Select) does implement add() and remove() using bit operations, however, he has a lot of rather inefficient code between add(), remove() and the code that actually does the bit operations.

    Personally, I make the bit fields myself. The code isn't that complicated, and I handle things that can_read() does not such EINTR. add() is implemented in terms of vec(...) = 1, etc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found