加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:17:12 所属栏目:PHP教程 来源:网络整理
导读:我的C应用程序有非常原始的Web前端.客户端(Web浏览器)进入php站点并使用参数填充表单.比(提交后)php调用exec和应用程序完成其工作. 应用程序的工作时间可能超过一分钟,并且需要相当大的RAM. 是否有可能检测到与客户端的断开连接(例如,在Web浏览器中关闭选项
我的C应用程序有非常原始的Web前端.客户端(Web浏览器)进入php站点并使用参数填充表单.比(提交后)php调用exec和应用程序完成其工作.
应用程序的工作时间可能超过一分钟,并且需要相当大的RAM.
是否有可能检测到与客户端的断开连接(例如,在Web浏览器中关闭选项卡).我想这样做,因为在断开连接后客户端将无法看到计算结果,所以我可以杀死应用程序并释放服务器上的一些RAM.

感谢您的任何帮助或建议.

解决方法

只要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扩展定义的 – 尽管我没有安装过没有其他的,所以我不确定.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读