PHP中sleep()和usleep()之间的区别 [英] Difference among sleep() and usleep() in PHP

查看:127
本文介绍了PHP中sleep()和usleep()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释一下PHP中sleep()usleep()有什么区别.

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

我已指示要使用以下脚本进行聊天应用程序以进行长时间拉动,但是在此脚本中,无论使用usleep(25000);还是不使用usleep(25000);

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"; 
}
?>

推荐答案

sleep的参数为秒,usleep的参数为微秒.除此之外,我认为它们是相同的.

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)仅睡眠0.025秒.

usleep(25000) only sleeps for 0.025 seconds.

这篇关于PHP中sleep()和usleep()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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