Sleep command in PHP returns non zero
I have a script, that is run as a part of my module in Drupal.
In this module, I use cURL functions to request html page from a remote website, then I parse the html content and write it to my database.
I wanted to have a delay between every request that I do to the remote server, hence I added sle开发者_运维百科ep()
in my PHP script.
function get_me_data()
{
for( 15 iterations)
{
$html = file_get_contents($search_url);
//parse html contents
$delay = mt_rand($interval1,$interval2); // interval1 = 0 , interval2=30
$retval = sleep($delay);
}
}
- function
get_me_data()
is initiated by cron job(hook_cron implemented in .module file). - I have also tried to flush streams before the usage of sleep command.
used
flush(); ob_flush();fflush(filepointer); ob_implicit_flush(true)
It so happens that after 4-5 iterations, sleep()
command returns NON ZERO value.
the script aborts in the next iteration when sleep()
is called
Let me know, if I am overlooking any aspect of sleep()
here.
I am using, PHP version 5.3 & Drupal 6
According to the docs http://php.net/manual/en/function.sleep.php can return non zero values.
If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of the WAIT_IO_COMPLETION constant within the Windows API). On other platforms, the return value will be the number of seconds left to sleep.
A comment in the sleep uses time_sleep_until() because it doesn't get interrupted.
精彩评论