stream_select() not working on regular files?

[email protected] ("Dennis J.")
Newsgroups php.general
Message-ID <[email protected]>
Hi,
I'm trying to implement something similar to "tail -f" in php but I'm 
running into a problem.
The issue occurs when I've reached the end of the file. From here I 
basically have to loop until new lines get appended to the file but I would 
like to respond immediately when then happens. There are three options that 
I can see:

1. Busy-loop
pro: I can process new lines immediately
contra: Excessivley CPU intensive => not a real option

2. add a sleep(1) to the loop
pro: No longer kills the CPU
contra: I might get a 1 second delay until I can process new lines

3. stream_select(array($fh),null,null,1)
pro: sleeps for one second but returns earlier if new data arrives
contra: doesn't seem to work in files?

Method 3 is the preferable one but doesn't seem to work:

$fh = fopen("testfile","r");
$r = array($fh);
while( ($n = stream_select($r,$w=null,$e=null,1)) == 1 ) {
     echo fgets($fh);
}

This program will loop forever because stream_select() will always return 1 
even at the end of the file.

Is there any other way to accomplish this?

Regards,
   Dennis
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.