我正在尝试执行服务器发送事件以在客户端页面处于活动状态时不断更新数据库。为了防止 mysql 负载过重,我决定使用 sleep(10) 来设置间隔。但我发现 sleep() 函数会导致性能问题。我想知道任何方法都可以代替睡眠来进行间隔。
面临的问题:
1.重新加载页面需要很长时间。
2.虽然睡眠其他页面无法处理。
以下是我目前使用的示例代码。
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$updated_time = date("Y-m-d H:i:s");
$sql = "UPDATE mytable SET alive='$updated_time' WHERE status='active'";
runSql($sql);
echo "data: {$updated_time} \n\n";
sleep(10);
flush();