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

Update: it would seem the problem is actually located within this part:

while(my $line = $buffer->sysreadline($fh)) { chomp($line); $self->_process($line); } }
and thus really has nothing to do with the actual select call...looks like I need to divert my attention to seeing what goes wrong in the sysreadline() call :/

Update 2 Well, what do you know, if sysreadline always appends a lineend to its return call, even when the actual line itself is empty, then it'll always evaluate to true, and the inner while loop above will never exit. Now if you'll all please excuse me, I need to find a really hard object to bang my head against for wasting your and my time in this way ;-)


Esteemed monks,

this has been baffling me for a few days already so I thought I might turn to you all for help. I have the following little while/select loop in my code:

sub run { while(1) { my @ready; print "about to check buffers\n"; unless(@ready = $buffer->ready) { select(undef, undef, undef, 0.001); next; } print "just after checking buffers\n"; foreach my $fh (@ready) { my $self = $objects{$fh}; while(my $line = $buffer->sysreadline($fh)) { chomp($line); $self->_process($line); } } } }
Note that $buffer in this case points towards a little thingamabob I wrote on top of IO::Select. Suffice to say that $buffer->ready performs an IO::Select->can_read with a timeout of 0.001. What's going wrong here is that the 2 print statements are executed once when the script is started and then never again. I know the while loop is being walked through time and time again, because everytime there is something available in @ready it is indeed processed in the second part of the loop. I've tried changing the 2 print statements to print explicitly to STDOUT but no such luck either. I even tried to blame my poor lil' WinXP box but then it turned out the FreeBSD server does the same. Any ideas on what this might be?


Remember rule one...

Replies are listed 'Best First'.
Re: select loop madness
by Zaxo (Archbishop) on Jul 19, 2005 at 08:37 UTC

    That behavior sounds really odd to me. I don't see why it would act that way. I'm suspicious of the global variables or closures that appear in run(). Have you tried writing this as a method of $buffer?

    Does the processing appear in blocks of all one handle at a time? That might make the loop only run once, if your sysreadline() method is highjacking select. I'd like to see how you've subclassed IO::Select. I suspect the odd behavior must originate there.

    Does select really work right on WinXP?

    After Compline,
    Zaxo

      Well, here's the actual module $buffer uses: I can't see anything in there that might cause this behavior. All it really does is create an artificial buffer so I could stuff the dirty work of processing unbuffered reads somewhere nicely out of sight.


      Remember rule one...