Can any body explain me what is the difference among sleep() and usleep() in PHP.
任何人都可以解釋一下PHP中sleep()和usleep()之間的區別。
I have directed to use following scripts to do chat application for long pulling but in this script I am getting same effect using usleep(25000); or without usleep(25000);
我已經指示使用以下腳本來進行長時間拉動的聊天應用程序,但是在這個腳本中我使用usleep獲得相同的效果(25000);或者沒有睡覺(25000);
page1.php
page1.php中
var lpOnComplete = function(response) {
console.log(response);
// do more processing
lpStart();
};
var lpStart = function() {
$.post('page2.php', {}, lpOnComplete, 'json');
};
$(document).ready(lpStart);
page2.php
使page2.php
$time = time();
while((time() - $time) < 30) {
// query memcache, database, etc. for new data
$data = getLatest();
// if we have new data return it
if(!empty($data)) {
echo json_encode($data);
break;
}
usleep(25000);
}
function getLatest() {
sleep(2);
return "Test Data";
}
?>
4 个解决方案
#1
36
The argument to sleep is seconds, the argument to usleep is microseconds. Other than that, I think they're identical.
睡眠的參數是秒,usleep的參數是微秒。除此之外,我認為它們是完全相同的。
sleep($n) == usleep($n * 1000000)
usleep(25000) only sleeps for 0.025 seconds.
usleep(25000)只能睡0.025秒。
#2
5
sleep() allows your code to sleep in seconds.
sleep()允許你的代碼在幾秒鍾內睡眠。
sleep(5); // sleeps for 5 seconds
睡眠(5); //睡5秒
usleep() allows your code with respect to microseconds.
usleep()允許你的代碼相對於微秒。
usleep(2500000); // sleeps for 2.5 seconds
usleep(2500000); //睡2.5秒
#3
2
usleep() is used to delay execution in "microseconds" while sleep() is used to delay execution in seconds. So usleep(25000) is 0.025 seconds.
usleep()用於在“微秒”內延遲執行,而sleep()用於在幾秒鍾內延遲執行。所以usleep(25000)是0.025秒。
Is there any difference between the two?
#4
0
One other difference is sleep returns 0 on success, false on error. usleep doesn't return anything.
另一個區別是成功時睡眠返回0,錯誤時返回false。 usleep不返回任何東西。