PHP进程执行超时

标签 php process timeout proc-open

我有以下代码:

/**
 * Executes a program and waits for it to finish, taking pipes into account.
 * @param string $cmd Command line to execute, including any arguments.
 * @param string $input Data for standard input.
 * @param integer $timeout How much to wait from program in msecs (-1 to wait indefinitely).
 * @return array Array of "stdout", "stderr" and "return".
 */
function execute($cmd,$stdin=null,$timeout=-1){
    $proc=proc_open(
        $cmd,
        array(array('pipe','r'),array('pipe','w'),array('pipe','w')),
        $pipes=null
    );
    fwrite($pipes[0],$stdin);                  fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);    fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);    fclose($pipes[2]);
    $return=proc_close($proc);
    return array(
        'stdout' => $stdout,
        'stderr' => $stderr,
        'return' => $return
    );
}

它有两个“问题”。

  • 代码是同步的;它会卡住,直到目标进程关闭。
  • 到目前为止,如果不发出不同类型的命令(例如 Linux 上的 $cmd >/dev/null &start/B $cmd(在 Windows 上)

我根本不介意“卡住”。我只需要实现该超时。

注意:该解决方案的跨平台兼容非常重要。同样重要的是, $cmd 不必更改 - 我正在运行一些复杂的命令,恐怕可能会出现一些问题,但是,这取决于修复的类型 -我很高兴听到这些,只是我更喜欢不同的选择。

我找到了一些可能有帮助的资源:

最佳答案

代码中有一些错误。

这确实有效:

function execute($cmd, $stdin = null, $timeout = -1)
{
    $proc=proc_open(
        $cmd,
        array(array('pipe','r'), array('pipe','w'), array('pipe','w')),
        $pipes
    );
    var_dump($pipes);
    if (isset($stdin))
    {
        fwrite($pipes[0],$stdin);
    }
    fclose($pipes[0]);

    stream_set_timeout($pipes[1], 0);
    stream_set_timeout($pipes[2], 0);

    $stdout = '';

    $start = microtime();

    while ($data = fread($pipes[1], 4096))
    {
        $meta = stream_get_meta_data($pipes[1]);
        if (microtime()-$start>$timeout) break;
        if ($meta['timed_out']) continue;
        $stdout .= $data;
    }

    $stdout .= stream_get_contents($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    $return = proc_close($proc);

    return array(
        'stdout' => $stdout,
        'stderr' => $stderr,
        'return' => $return
    );
}

关于PHP进程执行超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5309900/

相关文章:

Haskell——强制使用奇怪的递归类型进行严格评估

ruby - 在进行数据库调用的线程中使用 ruby​​ 超时

php - 带有 html 页面的动态数据库驱动网站

php 从字符串中获取子字符串

php - 如何在不使用外连接或不使用任何连接的情况下检索数据?

powershell - PowerShell 中的调用运算符到底做什么?

c# - FindWindow 和 SetForegroundWindow 替代品?

php - 在 php 中放置一个表格,第一行

process - 在 Elixir 任务中获取用户输入

Java套接字超时不适用