dongpeng7744 2015-11-17 07:40 采纳率: 100%
浏览 69
已采纳

杀死使用ignore_user_abort()的PHP脚本

Some time ago, I was looking for a method to keep a PHP script running when the user aborted. (When the user closed it's browser). I needed this for some long queries to complete even if the user decided to leave.

ignore_user_abort(1);
set_time_limit(0);

while (true) {
    // Do queries
}

How would I kill the above script?

Update

The getmypid() function has been suggested to me. I now implemented the following:

ignore_user_abort(1);
set_time_limit(0);

// Get process id
$pid = getmypid();

$stmt = "INSERT INTO processes (pid, started) VALUES ('".$pid."', NOW())";
while (true) {
    // Do Queries
    if ($query) {
        break;
    }
}
$stmt = "UPDATE processes SET ended=NOW() WHERE pid='".$pid."'";

I can now query the database to see hwat processes have not finished yet. I'll know that these processes are stuck.

  • 写回答

3条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 b站私信完整导出的方法
      • ¥15 边界图中的x、y轴解释
      • ¥30 如何用matlab求解复杂方程
      • ¥15 VUE TS中怎么调用第三方js
      • ¥15 Activiti如何将值传入textValue
      • ¥20 在linux里安装lummps时 在make mpi里找不到rpc
      • ¥25 python先到先得调度和轮询调度
      • ¥15 SDK Manager闪退
      • ¥15 intel i219v mac地址修改问题
      • ¥15 leetcode上面的题,求解