什么是PHP的睡眠()的实际用途? [英] What is a practical use for PHP's sleep()?

查看:184
本文介绍了什么是PHP的睡眠()的实际用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚查看了 sleep()



您在哪里使用此功能?



是它在那里让CPU在一个昂贵的函数中休息一下?



任何常见的陷阱?

解决方案



假设您已经构建了一个爬虫程序使用 curl / file_get_contents 来获得远程页面。现在你不想在短时间内以太多的请求轰炸远程服务器。所以你在连续的请求之间引入一个延迟。


$ b

sleep 以秒为参数,它的朋友 usleep 以微秒为参数,在某些情况下更合适。


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 pitfalls?

解决方案

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.

这篇关于什么是PHP的睡眠()的实际用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆