doubao6936 2013-10-24 05:26
浏览 79
已采纳

PHP中的sleep()和usleep()之间的区别

Can any body explain me what is the difference among sleep() and usleep() in PHP.

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);

page1.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" 
       type="text/javascript"></script>

<script>
var lpOnComplete = function(response) {
    console.log(response);
    // do more processing
    lpStart();
};

var lpStart = function() {
    $.post('page2.php', {}, lpOnComplete, 'json');
};

$(document).ready(lpStart);
</script>

page2.php

<?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"; 
}
?>
  • 写回答

5条回答 默认 最新

  • duanpa1898 2013-10-24 05:30
    关注

    The argument to sleep is seconds, the argument to usleep is microseconds. Other than that, I think they're identical.

    sleep($n) == usleep($n * 1000000)
    

    usleep(25000) only sleeps for 0.025 seconds.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?