【发布时间】:2015-05-09 00:44:24
【问题描述】:
我正在尝试使用长轮询从 MySQL 表中获取最新消息。
如果我没有收到任何新消息,我将编写脚本 sleep() 10 秒。
但在最坏的情况下,如果我在 10 分钟内没有收到任何新消息,脚本会显示 max_execution_time 已超出。
我将如何解决这个问题。谁能说我哪里出错了。
PS - 至少我现在不想使用 Websockets。
pullmessage.php
$notyetgot = true;
$data_msg = mysqli_query($con,"SELECT * FROM messages WHERE message_id>".$latest." ");
$n = mysqli_num_rows($data_msg);
if($n > 0){$notyetgot=false;}
while($notyetgot){
sleep(10);
$data_msg = mysqli_query($con,"SELECT * FROM messages WHERE message_id>".$latest." ");
$n = mysqli_num_rows($data_msg);
if($n > 0){$notyetgot = false;}
}
...
【问题讨论】:
-
不要那样做。重新设计流程,这样 Web 服务器就不会坐在那里等待“活动”线程。
-
你能澄清@RickJames 应该重新设计什么吗?能举个简单的例子吗
标签: php mysql ajax comet long-polling