note 114350 added to function.fread
| From: | fread at imperium dot be | Date: | Tue, 11 Feb 2014 17:13:17 +0000 |
| Subject: | note 114350 added to function.fread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198459@lists.php.net to get a copy of this message | ||
My script was based on example 3b, but used up 100% CPU when a timeout occurred that wasn't
"seen". This is very bad. So here's my code, hoping this will help people out there
with the same problem. Obviously first use $rPage = fsockopen(...) and fwrite($rPage,...) and such,
after which:
$sPage = ''; // the page goes in here
$iTimeout = 5; // set the timeout in seconds
stream_set_timeout($rPage,$iTimeout);
stream_set_blocking($rPage,0);
$fTimeout = microtime(true);
do {
if (($sRead = fread($rPage,8192))!==false and strlen($sRead)) {
$sPage .= $sRead; }
else {
usleep(10000); } // 0.01 second
$aInfo = stream_get_meta_data($rPage); }
while (!feof($rPage) and !$aInfo['timed_out'] and
microtime(true)-$fTimeout<$iTimeout);
fclose($rPage);
// now simply decompress and unchunk $sPage, if need be
Above code will make sure the timeout is used, because this isn't always detected properly. In
addition, the usleep() will keep the CPU in check.
----
Server IP: 83.137.20.135
Probable Submitter: 85.150.54.188
----
Manual Page -- http://php.net/manual/en/function.fread.php
Edit -- https://master.php.net/note/edit/114350
Del: integrated -- https://master.php.net/note/delete/114350/integrated
Del: useless -- https://master.php.net/note/delete/114350/useless
Del: bad code -- https://master.php.net/note/delete/114350/bad+code
Del: spam -- https://master.php.net/note/delete/114350/spam
Del: non-english -- https://master.php.net/note/delete/114350/non-english
Del: in docs -- https://master.php.net/note/delete/114350/in+docs
Del: other reasons-- https://master.php.net/note/delete/114350
Reject -- https://master.php.net/note/reject/114350
Search -- https://master.php.net/manage/user-notes.php