in reply to Re^2: Malfunctioning select() call on a FIFO
in thread Malfunctioning select() call on a FIFO
a simple "$fifodata = <FIFO>;" has always worked here (though I'll admit sysread() is probably better).
Is is better simply because using buffered IO with select is wrong.
The only reason I even tried using sysread() instead was in case <FIFO> was somehow leaving something on the FIFO
Leaving stuff on the FIFO is fine. <FIFO> is bad because it can return less than what it reads from the FIFO. That causes select to block when it shouldn't. For example,
use IO::Select qw( ); use IO::Handle qw( ); pipe(my $rfh, my $wfh) or die; $wfh->autoflush(1); if (fork()) { my $sel = IO::Select->new($rfh); while ($sel->can_read()) { my $got = <$rfh>; last if !defined($got); chomp $got; print("It took ", (time()-$got), " seconds to get the msg\n"); } } else { for (;;) { print($wfh time(), "\n") or die; print($wfh time(), "\n") or die; sleep(3); } }
It took 0 seconds to get the msg It took 3 seconds to get the msg It took 0 seconds to get the msg It took 3 seconds to get the msg It took 0 seconds to get the msg ...
But with sysread:
my $sel = IO::Select->new($rfh); my $buf = ''; while ($sel->can_read()) { sysread($rfh, $buf, 64*1024, length($buf)) or last; while ($buf =~ s/^(.*)\n//) { my $got = $1; print("It took ", (time()-$got), " seconds to get the msg\ +n"); } }
It took 0 seconds to get the msg It took 0 seconds to get the msg It took 0 seconds to get the msg It took 0 seconds to get the msg It took 0 seconds to get the msg It took 0 seconds to get the msg ...
I point out again that this select() call used to work, and has not been changed since then;
And select is still working properly. The problem has nothing to do with select.
because every select() call on the FIFO after the first command is read from it is saying that there is data on the FIFO to read
No, it's saying that the handle needs to be serviced. Specifically, it needs to be closed because it reached EOF. I explained this. Stop using select on a handle you're told is closed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Malfunctioning select() call on a FIFO
by Llew_Llaw_Gyffes (Scribe) on Jan 06, 2010 at 20:46 UTC | |
by ikegami (Patriarch) on Jan 06, 2010 at 21:46 UTC | |
by Llew_Llaw_Gyffes (Scribe) on Jan 06, 2010 at 21:52 UTC |