php sleep和usleep,PHP中的sleep()和usleep()之間的區別

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不返回任何東西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值