Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Non blocking read on a filehandle

by tadman (Prior)
on Jan 30, 2001 at 19:34 UTC ( [id://55249]=note: print w/replies, xml ) Need Help??


in reply to Non blocking read on a filehandle

If you're willing to get down and dirty, you can use select() like the rest of them. Earlier, reyjrar was asking about doing the same sort of thing on a socket. The same principles apply since file-handles and socket handles are really quite the same. That post is here.

I've extracted the relevant bits of that code and posted it here as an example on how to check up on a filehandle without blocking:
my ($block_size) = 10_000_000; # 10MB block size my ($timeout) = 0; # Don't wait. my ($rfd) = ''; # Initialize with string only # SYNOPSIS: open ($filehandle, "FILE or PIPE"); CheckHandle ($filehandle); sub CheckHandle { my ($filehandle) = @_; vec ($rfd, fileno($filehandle), 1) = 1; # Wait for something to happen, and make sure # that it happened to the right filehandle. if (select ($rfd, undef, undef, $timeout) >= 0 && vec($rfd, fileno($filehandle), 1)) { # Something came in! my ($buffer); read ($filehandle, $buffer, $block_size); return $buffer; } return; }
Explanation:
POSIX calls such as select() use a slightly different view of the world when compared with Perl. Most of the time Perl will convert for you automatically, but there are a few cases where you have to help out.

select() polls a number of file-handles (or sockets) and can report if they are ready to read, write, or if they have experienced an error. The documentation on select() is a little thin, and it will take a bit of experimentation to get a good handle on it. Note that select() can be configured to block or to be non-blocking, the trick is in the "$timeout" parameter. A zero timeout will put it into non-blocking mode.

In order to tell select which filehandles you want to monitor, vec() is used to create a bit-mask with bit number zero representing filehandle 0, bit 1 for filehandle 1, etc. These filehandles are a bit different from Perl filehandles, so a quick conversion with fileno() will get things on track.

This code uses "scalar"-style filehandles here because they are easier to pass between functions, and they work the same in most circumstances.

Replies are listed 'Best First'.
Re: Re: Non blocking read on a filehandle
by c-era (Curate) on Jan 30, 2001 at 21:42 UTC
    You set $timeout to 0, this will cause select to block until it can read, it doesn't set select to nonblocking like you sugested. You can set $timeout to 1 and then select will wait up to 1 second to see if anything comes in, and then will continue on with the next line of code. But, this will slow down your program as it will wait up to 1 second each time your loop is run.
Re (tilly) 2: Non blocking read on a filehandle
by tilly (Archbishop) on Jan 31, 2001 at 08:40 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-16 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found