Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Stumped with select->can_read (buffered)

by zentara (Archbishop)
on Aug 03, 2012 at 20:43 UTC ( #985349=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Stumped with select->can_read (buffered)
in thread Stumped with select->can_read

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


Comment on Re^3: Stumped with select->can_read (buffered)
Download Code
Re^4: Stumped with select->can_read (buffered)
by w1r3d (Initiate) on Aug 03, 2012 at 21:07 UTC

    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

      My concern now is whether 1024 is too small of a buffer that I could miss something (if that's possible)

      If there are more than 1024 bytes to be read, then the next call to can_read() will tell you about them.

      - tye        

      Would it be horrible (performance wise) to read one byte at a time until I reach a newline character, thus forming a real $line?

      It's been done before. :-) It certainly is inefficient. What I would do, is sysread as big of chunks as you can, then concat everything into a temp buffer, then split the buffer on newlines into an @array. If the scalar @array is greater than 1, then you have a line. Return the last @array element to your $temp_buffer, and process the rest of the @array elements as lines. It's called a sliding buffer, and you can google for "perl sliding buffer" for examples.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        sysread as big of chunks as you can, then concat everything into a temp buffer

        sysread can read onto the end of the buffer, avoiding the need to copy the data again:

        sysread( $fh, $buf, 16*1024, length($buf) );

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-07-31 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (254 votes), past polls