dougou8552 2009-05-20 04:15
浏览 133
已采纳

popen和文件操作

related to this question

my script basically runs fine but sometimes it stop responding at the fread function call and I can't seem to find the reason of the failure.

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

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

    pclose($handle);
}

example command

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

for now it is failing while cloning a large repository. the same problem persists even if I change fread to fgets.

Brief information about my php environment,

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

Running on ubuntu 8.04.2

EDIT: tried proc_open instead of popen and the script stuck at the same place. EDIT: replaced fread with stream_get_contents, but still stuck at the same place...

  • 写回答

1条回答 默认 最新

  • dsizmmwnm56437180 2009-05-20 07:05
    关注

    My current solution

    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!
    ";
        } else {
            echo "failed!
    ";
        }
    }
    

    the long waiting time occurred while calling fread is probably because access to the $handle is being blocked for popen to write output into it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)