gpt4 book ai didi

php - 轮询 Apache 服务器没有响应

转载 作者:行者123 更新时间:2023-11-29 07:48:14 24 4
gpt4 key购买 nike

当控制消息轮询的脚本正在运行时,我用来开发系统的 apache 服务器将不会响应请求。这仅发生在域级别,这意味着我可以向本地托管的任何其他应用程序发送 http 请求并获得响应。当我最终得到回复时,大约一分钟后。

这是 Js

window.fetch_messages = function ()
{
var last_message = $("div.message:last").attr('data-ai_id');
var last_message_status = $("p.message_status:last").text();
var project_id = getParameterByName('project-id');

$.ajax({
url:'/project_messages',
type:'POST',
data:{ project_id:project_id, latest_message:last_message, status:last_message_status },
timeout:50000,
async: true,
success:new_messages, // This upon completion also resends the request
error:function(data){ console.log(data); setTimeout(fetch_messages(),50000); }
});

}; // When On the page that uses this I call this function to start polling

这是服务器端代码

do 
{
// Check for status change
$status_change = $this->mentor_model->query_status($this->project_id, $this->last_message_id, $this->last_message_status, $_SESSION['user']);

// Check for new messages
$messages = $this->mentor_model->query_messages($this->project_id, $this->last_message_id);

// If there is a status update or new message.
if($messages || $status_change)
break;

usleep(1000000);
}
while(empty($messages) && empty($status_change));

echo json_encode(array("messages"=>$messages, "status"=>$status_change));
exit;

运行此操作时,服务器需要很长时间来处理任何请求(无论是 GET、POST 或其他 AJax 请求)。 iv 也尝试过更改这两个代码集,但没有效果,只要其轮询时间较长,服务器将需要很长时间来处理。

我是否有这个错误,或者是否有一些我应该更改的 apache 设置。在 Windows 8.1 上使用 xamp 也尝试了 wamp 没有任何变化

最佳答案

感谢史蒂文。答案直接取自 php 手册页的来源对于 session_write_close();

You can have interesting fun debugging anything with sleep() in it if you have a session still active. For example, a page that makes an ajax request, where the ajax request polls a server-side event (and may not return immediately).

If the ajax function doesn't do session_write_close(), then your outer page will appear to hang, and opening other pages in new tabs will also stall.

关于php - 轮询 Apache 服务器没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27155770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com