dqrnsg6439 2013-08-20 06:59 采纳率: 100%
浏览 70

带sleep()的脚本正在减慢服务器速度,导致Apache崩溃

I have a website that has messaging functionality between users. When a user is logged in, I use jQuery.ajax() to call a PHP script to check for new messages. To cut down on requests, my PHP script loops a call to the check_new_messages() function and if there are no new messages, I call sleep(5) inside the loop, so that it waits 5 seconds and then continues the loop. I check how long the script has been executing by using microtime() and if it exceeds 60 seconds, I return 0. The ajax will receive this value and then call the script again.

function check_message_queue($user) {
    $response = 0;
    $msgs = 0;
    $time_start = microtime(true);

    while (!$msgs = check_new_messages($user)) {
        sleep(5);
        $time_end = microtime(true);
        if ($time_end - $time_start >= 60)
            return $response;
    }

    // has new messages
    sleep(5);
    return $response;
}

My php.ini has max_execution_time set to 120.

At first everything works OK, but when I refresh the page there is about a 10 second delay and sometimes I'll get the PHP error "Max execution time of 30 seconds has been exceeded" followed by Apache crashing. My PHP max_execution_time is definitely set to 120 seconds, so I'm not sure what's going on.

I've never done anything like this before, so hopefully it's just some bad coding on my part.

Here is my JavaScript:

var request_new_messages = function() {
    $.ajax({
        url: 'messages/checkqueue',
        type: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        data: { id: 0 },
        complete: function() { request_new_messages(); },
        error: function(jqXHR, textStatus, errorThrown) { handle_error(); },
        success:
            function(data, textStatus, jqXHR) {
                if (textStatus == "success") {
                    if (!isNaN(data)) {
                        var response = parseInt(data);
                        if (response > 0)
                            alert('You have ' + response + ' new messages.');
                    }
                }
            }
    });
};
  • 写回答

2条回答 默认 最新

  • dreamy1992 2013-08-20 07:08
    关注

    You should not use PHP for that really. If you have an access to your server, i.e. it is not a share hosting or something like that, read about nodejs and socket.io. Besides that what is going on is that you are pausing the php every time once someone access it. Your script could be accessed several times per second. Imagine what will happen if you have 50 users. That could cause a crash of the apache. Can you please check the apache's logs and report the last few lines here (I mean before the crash).

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法