in reply to select() and input buffering
I suspect you are suffering from the problem I described in Re^3: Malfunctioning select() call on a FIFO. You need to use sysread. It reads no more than is currently available, and it doesn't buffer data outside of select's field of vision.
Note: I use IO::Select below since it's a thin layer that's much easier to use.
use IO::Select qw( ); # I like big blocks and and I can not lie. # Benchmarking might just deny. use constant BLK_SIZE => 64*1024; my $sel = IO::Select->new($fh); my $buf = ''; while ($sel->can_read()) { my $rv = sysread($fh, $buf, BLK_SIZE, length($buf)); if (!defined($rv)) { ... Handle error. Don't forget $buf might not be empty. ... $sel->remove($fh); } if (!$rv) { ... Handle EOF. Don't forget $buf might not be empty. ... $sel->remove($fh); } while ($buf =~ s/^(.*)\n//) { my $msg = $1; my $close = process_msg($msg); $sel->remove($fh) if $close; } }
Of course, if you have multiple handles, you'll need multiple buffers. A hash keyed by fileno is useful.
Threads solve this more simply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: select() and input buffering
by declan (Initiate) on Apr 14, 2011 at 04:09 UTC | |
by ikegami (Patriarch) on Apr 14, 2011 at 05:17 UTC | |
by declan (Initiate) on Apr 15, 2011 at 04:53 UTC | |
by ikegami (Patriarch) on Apr 15, 2011 at 20:22 UTC |