Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Stumped with select->can_read (buffered)

by tye (Cardinal)
on Aug 03, 2012 at 19:17 UTC ( #985326=note: print w/ replies, xml ) Need Help??


in reply to Stumped with select->can_read

while(@ready = $sel->can_read) #... $line = <$fh>;

Don't mix buffered I/O with select. can_read() doesn't return a handle because there is nothing currently left to read from the file handle. But <$fh> left plenty of stuff to be read in the buffer.

- tye        


Comment on Re: Stumped with select->can_read (buffered)
Select or Download Code
Re^2: Stumped with select->can_read (buffered)
by w1r3d (Initiate) on Aug 03, 2012 at 20:18 UTC

    That makes sense. Would I have to use read() in this case?

    I tried doing:

    while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; my $buf = ""; while(read($fh, $buf, 1024)) { $line .= $buf; } } }

    Didn't work for me, maybe because LENGTH (=1024) is too big? Sorry, I'm not familiar with read(), as you can see :/

    EDIT: nvm. I'm reading the link you posted now. Will report back if I still can't figure it out. Thanks!

      It's best to use sysread, but you will have to figure out a way to detect line endings. Maybe after concating the sysread data into a temp buffer then pull off lines with a regex or split. Read perldoc -f sysread. It will read as much as it can in a non-blocking manner. This will try to read in 1024 byte chunks, but will read less if that is all that is there.
      while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; my $buf = ""; # while(read($fh, $buf, 1024)) while( my $bytes_read = sysread( $fh, $buf, 1024 ) > 0 ) ) { print "$bytes_read\n"; { $line .= $buf; } } }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        I tried doing that, but it didn't seem to work. My guess is that because one particular $fh is very busy, the while loop never ends?

        This *seems* to work at the moment:

        while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; sysread( $fh, $line, 1024 ); } }

        My concern now is whether 1024 is too small of a buffer that I could miss something (if that's possible), or missing a newline character because of the buffer size. Would it be horrible (performance wise) to read one byte at a time until I reach a newline character, thus forming a real $line?

        Thanks again!

        Pedro

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985326]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-07-03 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (49 votes), past polls