有关

此问题

我的脚本基本上运行正常,但有时它停止在fread函数调用响应,我似乎无法找到失败的原因。

private function run_lengthy_job($command, $message) {
    for($handle = popen($command, 'r'); !feof($handle); sleep(2)) {
        printf("[%s]\t%s\n", time(), $message);

        // log the operation
        exec(sprintf('logger "%s"', 
            escapeshellarg(fread($handle, 1024))));
    }

    pclose($handle);
}

示例命令

hg clone -v --debug http://some.repository.com/hgwebdir.cgi/some_repo some_repo

有关而克隆一个大的库现在它失败。即使我改变的fread到与fgets同样的问题仍然存在。

约我的PHP环境的简要信息,

PHP 5.2.4-2ubuntu5.6 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 17 2009 14:29:38) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
    with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans

运行在Ubuntu 8.04.2

编辑:尝试proc_open代替POPEN和停留在同一个地方的脚本。 编辑:更换FREAD与stream_get_contents,但仍停留在同一个地方...

有帮助吗?

解决方案

我的当前的解决方案

private function run_lengthy_job($command, $message) {
    printf("%s\t", $message);

    for($handle = popen($command, 'r'),
        stream_set_blocking($handle, FALSE),
        stream_set_timeout($handle, 3); 
        !feof($handle); sleep(1)) {

        echo '.';

        exec(sprintf('logger "%s"', 
            escapeshellarg(stream_get_contents($handle))));
    }

    $exit_status = pclose($handle);

    if(pcntl_wifexited($exit_status) 
        && pcntl_wexitstatus($exit_status) == 0) {
        echo "done!\n";
    } else {
        echo "failed!\n";
    }
}

在调用FREAD可能是因为访问$处理被封锁POPEN输出写入到它发生很长的等待时间。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top