note 118619 added to function.stream-set-timeout
| From: | burninleo at gmx dot net | Date: | Sat, 09 Jan 2016 23:06:29 +0000 |
| Subject: | note 118619 added to function.stream-set-timeout | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204631@lists.php.net to get a copy of this message | ||
Another note alread states that blocking-reads may be an issue, if the counterpart responds very
slowly - or not at all. The stream timeout may not work as expected in such a situation.
However, php.net provides very little information on how to use non-blocking reading operations.
Here's a code sample:
<?php
stream_set_timeout($c, $timeout);
$data = '';
while (is_resource($c) && !feof($c)) {
// Use non-blocking reading for first loop
if (($data === '') and ($timeout > 0)) {
stream_set_blocking($c, false);
$endtimeOut = time() + $timeout;
$str = '';
while ((time() < $endtimeOut) and (strlen($str) < 515) and !feof($c)) {
sleep(1); // Note: This may require tuning
$str.= fgets($c, 515);
}
// Handling first-read timeout
if (time() >= $endtimeOut) {
trigger_error('Timeout', E_USER_WARNING);
break;
}
stream_set_blocking($c, true);
} else {
$str = fgets($c, 515);
}
$data.= $str;
// Handling of "traditional" timeout
$info = stream_get_meta_data($c);
if ($info['timed_out']) {
trigger_error('Timeout', E_USER_WARNING);
break;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 46.128.22.170
----
Manual Page -- http://php.net/manual/en/function.stream-set-timeout.php
Edit -- https://master.php.net/note/edit/118619
Del: integrated -- https://master.php.net/note/delete/118619/integrated
Del: useless -- https://master.php.net/note/delete/118619/useless
Del: bad code -- https://master.php.net/note/delete/118619/bad+code
Del: spam -- https://master.php.net/note/delete/118619/spam
Del: non-english -- https://master.php.net/note/delete/118619/non-english
Del: in docs -- https://master.php.net/note/delete/118619/in+docs
Del: other reasons-- https://master.php.net/note/delete/118619
Reject -- https://master.php.net/note/reject/118619
Search -- https://master.php.net/manage/user-notes.php