PHP - 在客户端断开连接时杀死exec

时间:2012-07-09 11:28:57

标签: php exec kill disconnect


我的C ++应用程序有非常原始的Web前端。客户端(Web浏览器)进入php站点并使用参数填充表单。比(在提交后)php调用 exec 并且应用程序完成其工作。 应用程序的工作时间可能超过一分钟,并且需要相当大的RAM。

是否有可能检测到与客户端的断开连接(例如,关闭Web浏览器中的选项卡)。我想这样做,因为在断开连接后客户端将无法看到计算结果,所以我可以杀死应用程序并释放服务器上的一些RAM。

感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:2)

只要C ++程序在运行时产生输出,而不是在终止之前生成所有输出,请使用passthru()而不是exec()

这会导致PHP在生成内容时将输出刷新到客户端,这允许PHP检测客户端何时断开连接。当客户端立即断开连接并终止子进程时,PHP将终止(只要未设置ignore_user_abort())。

示例:

<?php

  function exec_unix_bg ($cmd) {
    // Executes $cmd in the background and returns the PID as an integer
    return (int) exec("$cmd > /dev/null 2>&1 & echo $!");
  }
  function pid_exists ($pid) {
    // Checks whether a process with ID $pid is running
    // There is probably a better way to do this
    return (bool) trim(exec("ps | grep \"^$pid \""));
  }

  $cmd = "/path/to/your/cpp arg_1 arg_2 arg_n";

  // Start the C++ program
  $pid = exec_unix_bg($cmd);

  // Ignore user aborts to allow us to dispatch a signal to the child
  ignore_user_abort(1);

  // Loop until the program completes
  while (pid_exists($pid)) {

    // Push some harmless data to the client
    echo " ";
    flush();

    // Check whether the client has disconnected
    if (connection_aborted()) {
      posix_kill($pid, SIGTERM); // Or SIGKILL, or whatever
      exit;
    }

    // Could be done better? Only here to prevent runaway CPU
    sleep(1);

  }

  // The process has finished. Do your thang here.

要收集程序的输出,请将输出重定向到文件而不是/dev/null。我怀疑你需要安装pcntl以及posix,因为PHP手册表明SIGxxx常量由pcntl扩展名定义 - 尽管我从来没有一个安装没有另一个,所以我不确定。