IO::Select->can_read returns immediately

B

Brian T Glenn

I am attempting to use IO::Select to poll a FIFO created by mkfifo. The
FIFO has absolutely no writers attached to it. The following is the
code:

$fifo = IO::File->new();
fatal("Could not open FIFO for reading: $!") unless $fifo->open($cfg{'General'}{'FIFO'});

# Prepare to enter the main loop
$loop = IO::Select->new($fifo);
do_log(1,"Entering processing loop\n");
while(1) {
@tasklist = $loop->can_read();
foreach my $fh (@tasklist) {
if ($fh == $fifo) {
do_log(5,"Received a message from the FIFO");
}
else {
do_log(5,"Receiving a message from another source");
}
}
}

__END__

Shouldn't the can_read() method block until data appears on the FIFO? I
have tried it both with and without a timeout, and the process spins
indefinitely, filling the syslog with "Received a message from the FIFO".

I never receive the message "Receiving a message from another source" in
the syslog. There are also no other filehandles attached to the
IO::Select object.

Thanks in advance,
 
X

xhoster

Brian T Glenn said:
I am attempting to use IO::Select to poll a FIFO created by mkfifo. The
FIFO has absolutely no writers attached to it. The following is the
code:

Your code is not runnable. If I make my best guesses in order to make it
runnable, it does not display the symptoms you report.

$fifo = IO::File->new();
fatal("Could not open FIFO for reading: $!") unless
$fifo->open($cfg{'General'}{'FIFO'});

What is in $cfg{'General'}{'FIFO'} ?

In my hands, the opening of a fifo with absolutely no writers attached
blocks forever. Not the read, but the open itself.

Xho
 
D

delink

Jim said:
Brian T said:
I am attempting to use IO::Select to poll a FIFO created by mkfifo. The
FIFO has absolutely no writers attached to it. The following is the
code: [snip code]
Shouldn't the can_read() method block until data appears on the FIFO? I
have tried it both with and without a timeout, and the process spins
indefinitely, filling the syslog with "Received a message from the FIFO".

Yes, since can_read is not blocking, there must be data ready to read
on the FIFO. If you don't actually read the data from the FIFO, then it
will still be ready the next time you call can_read. Try reading the
data:

print <$fh>;

This was my problem. In my quick attempt to test the loop, I forgot to
actually read the data, causing it to spin. Thanks for pointing out the
seemingly obvious to me.
can_read will either return an empty list or a list with $fifo as its
only element. There is no way you can ever print "Receiving a message
from another source".

It is actually possible in the grand scheme of the code as extra
filehandles will be added as messages come into the FIFO, but the way
it is currently written, that is correct.

Thank you for the help,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top