Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Using select and IO::Select

by Zaxo (Archbishop)
on Jul 04, 2004 at 20:15 UTC ( [id://371745]=note: print w/replies, xml ) Need Help??


in reply to Using select and IO::Select

duff++, good job and much needed.

One of the great things about select is its efficiency. Instead of busying around a polling loop, select lets your process go to sleep until there is something to do. That does not speed up the process where select is, but rather frees its time slice for all the other processes you and other users may run. It's the neighborly thing to do, and good for throughput.

There are some more questions I think could be covered. I don't necessarily know the answers.

  • I/O methods: When can Perl's buffered I/O methods be used with select? The perldoc warns that sysread (and by extension, syswrite) is necessary, except as provided by POSIX. What does POSIX permit?
  • Signal handling: Do signals awake a sleeping select? Does a select timeout affect a pending alarm?

    (Added) This it readily checked with a few one-liners.

    $ perl -e'alarm 1;printf "Num: %d\tTime left: %f\n", select undef, und +ef, undef, 3.0' Alarm clock $
    shows that setting timeout in select does not interfere with SIGALRM and that signals will awake pending select.
    $ time perl -e'alarm 5;printf "Num: %d\tTime left: %f\n", select undef +, undef, undef, 3.0' Num: 0 Time left: 0.000000 $
    shows that having an alarm set does not interfere with select timing.
    $ perl -e'$SIG{ALRM}=sub {};alarm 1;printf "Num: %d\tTime left: %f\n", + select undef, undef, undef, 3.0' Num: -1 Time left: 2.000000 $
    shows that catching a signal will jolt select into returning with -1 in the number slot. On Linux the time left value would be useful in graceful recovery from such interruptions.

  • Return values: What's a good use for the number of ready channels? What systems return something useful for the time remaining? Linux does, are there others?

    (Added) Truth or not of the number tells whether the return from select was due to a timeout. As we saw, -1 means return due to interruption by catching signal. The number can be decremented with each channel handled to enable a quick test for completion. The timeleft value appears to be useful only on Linux. $ perl -e'printf "OS: %s\tNum: %d\tTime left: %f\n", $^O, select undef, undef, undef, 1.5' gives for several systems,
    OS: linux Num: 0 Time left: 0.000000 (Zaxo)
    OS: freebsd Num: 0 Time left: 1.500000 (sporty)
    OS: solaris Num: 0 Time left: 1.500000 (sporty)

    Thanks to sporty for his assistance with that.

  • IO::Select:: Does the very welcome sugar coating relax the restrictions on I/O methods?
  • IO::Select: Does IO::Select return IO::* objects or just whatever globlike things you add?
  • IO::Select: A more complicated example would be welcome. Something with error handling would be good.
  • Applications: What is a good way to get per-handle switching of I/O? That is needed for many sorts of servers, where responses to requests need to be directed to a particular channel.
  • Applications: Adding and removing handles on the fly. This is needed for servers which open new sockets in response to requests to a well-known port.

Again, this is a very good job, and welcome information. You can run your pod through pod2html to get a good format for posting here.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Using select and IO::Select
by tachyon (Chancellor) on Jul 05, 2004 at 00:46 UTC

    Signals are unsafe - ie they basically interrupt anything with no regard to whether it is a good idea/time or not. See Re: Dormus interruptus if you want to test it but they interrupt selects before timeout expiry.

    cheers

    tachyon

Re^2: Using select and IO::Select
by duff (Parson) on Jul 06, 2004 at 14:41 UTC

    Thanks and thanks. If you do know any of the answers to your questions you could send them to me and make my life slightly easier :) I plan on updating the tutorial over the coming week by addressing the questions you raise (and any others I can think of).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found