What is a practical use for PHP's sleep()?

前端 未结 13 2523
时光说笑
时光说笑 2020-12-23 19:17

I just had a look at the docs on sleep().

Where would you use this function?

Is it there to give the CPU a break in an expensive function?

Any common

相关标签:
13条回答
  • 2020-12-23 20:11

    I wouldn't typically use it for serving web pages, but it's useful for command line scripts.

    $ready = false;
    do {
      $ready = some_monitor_function();
      sleep(2);
    } while (!$ready);
    
    0 讨论(0)
  • 2020-12-23 20:11

    This is a bit of an odd case...file transfer throttling.

    In a file transfer service we ran a long time ago, the files were served from 10Mbps uplink servers. To prevent the network from bogging down, the download script tracked how many users were downloading at once, and then calculated how many bytes it could send per second per user. It would send part of this amount, then sleep a moment (1/4 second, I think) then send more...etc.

    In this way, the servers ran continuously at about 9.5Mbps, without having uplink saturation issues...and always dynamically adjusting speeds of the downloads.

    I wouldn't do it this way, or in PHP, now...but it worked great at the time.

    0 讨论(0)
  • 2020-12-23 20:12

    One of its application is, if I am sending mails by a script to 100+ customers then this operation will take maximum 1-2 seconds thus most of the website like hotmail and yahoo consider it as spam, so to avoid this we need to use some delay in execution after every mail.

    0 讨论(0)
  • 2020-12-23 20:15

    Another example: You're running some sort of batch process that makes heavy use of a resource. Maybe you're walking the database of 9,000,000 book titles and updating about 10% of them. That process has to run in the middle of the day, but there are so many updates to be done that running your batch program drags the database server down to a crawl for other users.

    So you modify the batch process to submit, say, 1000 updates, then sleep for 5 seconds to give the database server a chance to finish processing any requests from other users that have backed up.

    0 讨论(0)
  • 2020-12-23 20:17

    Super old posts, but I thought I would comment as well. I recently had to check for a VERY long running process that created some files. So I made a function that iterates over a cURL function. If the file I'm looking for doesn't exist, I sleep the php file, and check again in a bit:

    function remoteFileExists() {
        $curl = curl_init('domain.com/file.ext');
    
        //don't fetch the actual page, you only want to check the connection is ok
        curl_setopt($curl, CURLOPT_NOBODY, true);
    
        //do request
        $result = curl_exec($curl);
    
        //if request did not fail
        if ($result !== false) {
            //if request was ok, check response code
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
    
            if ($statusCode == 404) {
                  sleep(7);
                  remoteFileExists();
            }
            else{
                echo 'exists'; 
            }
        }
    
        curl_close($curl);
    
    }
    
    echo remoteFileExists();
    
    0 讨论(0)
  • 2020-12-23 20:18

    I had to use it recently when I was utilising Google's Geolocation API. Every address in a loop needed to call Google's server so it needed a bit of time to receive a response. I used usleep(500000) to give everything involved enough time.

    0 讨论(0)
提交回复
热议问题