Basic "IO::Select" problem

E

Ed W

I have been staring at this for a day now (durr), but I just can't see what
is wrong with this code: (apart from poor coding style, but it's a
snippet...)

It's running under windows XP with activestate perl 5.6.1. However, the
code below always returns an empty array from the select call? There is
definitely something to read from the filehandle, if I comment out the IF
statement, then I happily get the first 10 bytes read from my other perl
program

The bigger picture is that the other perl script is a long running program
which occasionally returns output to stdout. I want the wrapper program to
do non-blocking IO to read stdout from the child process, however, select
doesn't appear to be letting me know that there is stuff to read, and simply
doing sysread works, but then blocks as soon as there is nothing left to
read (it unblocks occasionally when more data arrives)

The even larger picture is that I was to log this stuff in a text box in TK,
but I haven't found the $window->fileevent stuff to work for either tcp
sockets, or filehandles, this seems to be a known issue with Win32 though?
In the meantime I just want something running in the idle loop in order to
get something working today and I will re-work it later in something better.

Thanks for any pointers

Ed W



use IO::File;
use IO::Select;

my $H = IO::File->new("multi-client16.pl |");
$H->blocking(0);
$s = IO::Select->new();
$s->add($H);


my @readable = $s->can_read(0);
if (@readable) {
# We never get here? Why?
$H->sysread($ARG, 10);
print $ARG;
}
 
E

Ed W

I made a few changes to the code and got something back. Since I
don't have a real understanding of what multi-client16.pl is suppose
to output, I used the Unix 'ls' command for a test. I added comments
to the new code below to point out what I did. I anyone sees an error
in what I did, please let me know. It works, but it may have some
pitfalls that are unknown to me.

Thanks James.

Actually, I should point out that everything works ok if the input has an
"eof" in it. So the ls command works fine, instead create a short program
which runs in an infinite loop, does an ls, then waits 90 seconds, then runs
ls, etc, etc.

I found that the select statement NEVER blocked, and always returned a null
array...? I will have a closer look at your code to see if it does
something different, but to my eyes, it looked the same?

Note also that this is a win 32 question. It is highly likely that under
unix the non blocking stuff works, but this certainly is fraught with
problems under win32. It may also be that select is not working properly
for file handles under win32?

Thanks

Ed
 
E

Edward Wildgoose

Benjamin Goldberg said:
That's because you pass in a timeout of 0. Change it to a postive
number, or to undef(), and it will work.

I have already tried with (1), but no difference. I thought that 0 was
acceptable, for seeing if anything was waiting, but returning immediately?
Oh, and you should be sysread()ing from $H, not from $ARG.

I am I think?

$H->sysread($somejunk, 10);


Thanks for you help (again!)

Ed
 
E

Edward Wildgoose

I do actually wonder if this is a win32 issue though. Does the code look
roughly plausible?

I will recode two scripts which can be demo-ed and resend. Thanks all
 

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,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top