Re: stream_select() not working on regular files?

From: Date: Thu, 04 Feb 2010 01:20:35 +0000
Subject: Re: stream_select() not working on regular files?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-301808@lists.php.net to get a copy of this message
On 02/04/2010 02:03 AM, Ashley Sheridan wrote:
On Thu, 2010-02-04 at 01:41 +0100, Dennis J. wrote:
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
I thought that once it reached the end of the file, it will return a 0 indicating no new activity?
That's what I thought too but apparently that is not the case.
Although, surely you want the loop to continue forever, so that new entries added to the end of the file are shown as soon as they appear.
Yes the loop is supposed to continue forever in the final version. In fact I what I'm trying to get at is a "tail -F" which means I will repeatedly reopen the file to check if it has been replaced by a new one. I just simplified the problem above to get rid of all the additional complexity and concentrate on the specific problem I have. My expectation was that once the end of the file is reached (i.e. fgets() has consumed all lines) stream_select() should wait for 1 second (in the above example) and if nothing happens with the file in that second it should return 0. But that doesn't happen. Regards, Dennis

« previous php.general (#301808) next »