home | contact us
» php » PHP Random Sleep Function with Flush

BLOG CATEGORY: php scraping


Sometimes you want your script to pause for a short period of time before repeating a loop or proceeding to the next step. This may be to reduce server load or even to simulate the natural pauses that a person would make whilst browsing a site. This kind of thing is especially true if you are building a system to scrape content such as product information from a suppliers web site and you do not want to hammer their server to death and get your IP banned.

 function sleep_flush($chunks=10){

 	$c=0;

 	while($c < $chunks){

 		$rand = rand(2000000, 6000000);

 		echo '<br> . . . sleeping for ' . round(($rand / 1000000),2) . ' seconds . . . zzzzzzzzzzzzzz<br>';

 		flush();

 		usleep($rand);

 		$c++;

 	}

}

This function will do just that for you. Also it has a built in flush which will help in preventing the script from being regarded as timed out if you are running it from the web browser.


Click Here to Contact Us about PHP Random Sleep Function with Flush
 

Comments

7 Comments

7 Responses to “PHP Random Sleep Function with Flush”

  1. good script but flush() is not acting as he says..;(

  2. admin says:

    you may have output buffering turned on

    try running

    ob_end_flush();

    at the top of your script

  3. James says:

    Thanks I was looking for something like this as I was using sleep but it still seemed to go to fast on some parts.

    I am using your function like this incase someone else wanted to know

    sleep_flush(1);

  4. Vish says:

    Thanks. It worked.

  5. James says:

    I have tried this but it seems to jam the script from wokring some times works some times does not work when used in a loop.

    Is there an alternative to usleep or sleep at can be used in a loop ?

  6. James says:

    Forgot to say I am not running it from a browser but as a cron.

  7. Joanna says:

    Hallo,
    I am using xampp, when i run your function, it returns nothing, do i have to install something else?

Leave a Reply

rss icon