Re: [PHP] stream_select() not working on regular files?
On 02/04/2010 06:18 AM, Eric Lee wrote: On Thu, Feb 4, 2010 at 9:20 AM, Dennis J.denni...@conversis.de wrote: 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 totail -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. Dennis I have just been bulit a simple test script. It works for me with a feof call if the stream was at end of file. But I'am not sure that is that what you want ! ?php $fp = fopen('test.xml', 'r'); $arr = array($fp); $w = $e = null; while (($result = stream_select($arr, $w, $e, 1)) !== false) { $line = fgets($fp); if (!empty($line)) { echo $line; } else { if (feof($fp)) echo 'eof',\n; fclose($fp); $fp = null; break; } } This script terminates before it hits the actual problem. The issue is that once I've hit the EOF I need to continue the loop using the stream_select() waiting for new data. Regards, Dennis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stream_select() not working on regular files?
Dennis J. wrote: The issue is that once I've hit the EOF I need to continue the loop using the stream_select() waiting for new data. AFAIK you can't; you see stream_select checks to see if something will be blocked; if EOF is reached it considers this as not blocked; in other words the second you hit a file EOF stream_select will continue to instantly return a positive (without the wait of 1 second). As far as I know every related function will always return the second EOF is hit; meaning that the -f functionality you want can only be gained by adding in the sleep (but i think every line reading function will still instantly return with an empty line) - so maybe you just need to proc_open to tail -f and read the stream with PHP - as it won't send an EOF - example: ?php $stream = popen( 'tail -f access.log' , 'r' ); while( $line = fgets($stream) ) { echo $line; } pclose( $stream ); // won't get here unless an error i guess.. good luck! Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] stream_select() not working on regular files?
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stream_select() not working on regular files?
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? 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. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] stream_select() not working on regular files?
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 totail -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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stream_select() not working on regular files?
On Thu, Feb 4, 2010 at 9:20 AM, Dennis J. denni...@conversis.de wrote: 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 totail -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. Dennis I have just been bulit a simple test script. It works for me with a feof call if the stream was at end of file. But I'am not sure that is that what you want ! ?php $fp = fopen('test.xml', 'r'); $arr = array($fp); $w = $e = null; while (($result = stream_select($arr, $w, $e, 1)) !== false) { $line = fgets($fp); if (!empty($line)) { echo $line; } else { if (feof($fp)) echo 'eof',\n; fclose($fp); $fp = null; break; } } Regards, Eric, Regards, Dennis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stream_select
Damien Churchill schreef: Is there a known issue with using stream_select with apache2 mod_php? I have a script (http://php.pastebin.com/m601965d) that when run with mod_php doesn't work, but when run with php-cgi from the command line works fine. define doesn't work. what exact php apache version? are you using shared hosting thats blocking all sorts of stuff form being done via the apache process? have you checked bugs.php.net for anything resembling your problem? you seem to be trying to connect to an imap server, have you tried the imap_*() functions as an alternative? http://php.net/imap Thanks in advance, Damien -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] stream_select
Is there a known issue with using stream_select with apache2 mod_php? I have a script (http://php.pastebin.com/m601965d) that when run with mod_php doesn't work, but when run with php-cgi from the command line works fine. Thanks in advance, Damien
[PHP] stream_select problem with signals
I'm rewriting an API to access OneWire Net, i have a problem with select and signals. The class will support both types of sockets, trought BSD sockets and with streams. My problem is that when the PHP app is in socket select and a signal arrives it threat it as a error, so i can fix in socket_select using socket_last_error in this form: $n = socket_select($_read, $_write, $_except, $sec, $usec); if($n === false) { if(socket_last_error($this-_sock) == 0) { // signal break the select $timeout -= (microtime(true) - $start); } else { // Error, not interrupted by signal return false; } } else { return $n; } This work, but i can't determine if a signal interrupted the stream_select, there is a way to detect this? Regards. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php