如何暂停PHP中的代码执行以及等待时会发生什么
If I want to suspend code execution in PHP do I use sleep()
?
If so, what exactly happens on the client side while the number of seconds passed to sleep()
are running? Nothing? It just waits for a response from the webserver? Or does it tell the browser - "OK, I got your request, but you will have to wait X seconds until I send you a response." ?
Thank you.
如果我想在PHP中暂停代码执行,我是否使用 谢谢。 p>
div> sleep() code>?\ 那么,当传递给
sleep() code>的秒数正在运行时,客户端究竟发生了什么? 没有? 它只是等待来自网络服务器的回复? 或者它告诉浏览器 - “好的,我收到了您的请求,但您必须等待X秒才能发送回复。” ? p>
sleep() simply suspends the script for the specified time interval. Internally it tells the OS "I'm going to sleep, wake me in X seconds". Unless the PHP script outputs a message to the client-side browser that it is going to sleep, the browser will simply hang until it decides the connection's dead, the network connection dies from lack of activity, or your script wakes up and resumes operations. sleep() is a purely server-side operation, and does NOT change anything on the client.
The client just waits. It is not informed in any way that you're wasting its time :-)
For one thing I am sure, a web server can not know in how many seconds a script will end, imagine the script working on a heavy mysql query, how could it tell when this will end.
If I want to suspend code execution in PHP do I use sleep()?
It is generaly true, however every case is different and maybe for some case an other solution could be found (maybe ajax?! depends..)