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

前端 未结 13 2519
时光说笑
时光说笑 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

    One place where it finds use is to create a delay.

    Lets say you've built a crawler that uses curl/file_get_contents to get remote pages. Now you don't want to bombard the remote server with too many requests in short time. So you introduce a delay between consecutive requests.

    sleep takes the argument in seconds, its friend usleep takes arguments in microseconds and is more suitable in some cases.

提交回复
热议问题