duanlie2709 2015-05-14 00:45
浏览 220
已采纳

在PHP中对popen / fgets施加时间限制

I want impose a time limit to a process reading using fgets opened by popen in PHP.

I have the next code:

$handle = popen("tail -F -n 30 /tmp/pushlog.txt 2>&1", "r");
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "data: ".$buffer."
";
    @ob_flush();
    flush();
}
pclose($handle);

I tried without success:

set_time_limit(60);
ignore_user_abort(false);

The process is as follow:

  1. The browser send a GET request waiting for a Answer in HTML5 Server side events format.
  2. The request is received by AWS Load Balancer and is forwarded to EC2 instances.
  3. The answer is the last 30 lines of the file
  4. The browser receive it in 30 messages and the connection is persisted.
  5. If tail command sends a new line it is returned else fgets wait undefined time until new line is returned from tail command.
  6. AWS Load Balancer after 60 seconds of network inactivity (No new lines in 60 seconds) closes the connection to the browser. The connection to EC2 instance is not closed.
  7. The browser detect that the connection is closed and it opens a new connection, the process go back to step 1.

AS this steps describe, the connection between AWS Load Balancer and EC2 instance is never closed, after a few hours/days there is hundreds and hundreds of tail and httpd process running and the server start not answering.

Of course it appear to be a AWS Load Balancer bug, but I don't want start a process to gain the attention from Amazon and wait for a fix.

My temporary solution is do a sudo kill tail to kill the process before the server becomes unstable.

I think PHP doesn't stop the script because PHP is "blocked" waiting for fgets to finish.

I know that the time limit of AWS Load Balancer is editable, but I want keep in the default value, even a higher limit is not going to fix the problem.

I don't know if I need change the question to How to execute a process in linux with a time limit / timeout?.

PHP 5.5.22 / Apache 2.4 / Linux Kernel 3.14.35-28.38.amzn1.x86_64

  • 写回答

2条回答 默认 最新

  • doujishao8793 2015-05-14 04:25
    关注

    Tested with PHP 5.5.20:

    //Change configuration.
    set_time_limit(0);
    ignore_user_abort(true);
    
    //Open pipe & set non-blocking mode.
    $descriptors  = array(0 => array('file', '/dev/null', 'r'),
                          1 => array('pipe', 'w'),
                          2 => array('file', '/dev/null', 'w'));
    $process      = proc_open('exec tail -F -n 30 /tmp/pushlog.txt 2>&1',
                                    $descriptors, $pipes, NULL, NULL) or exit;
    $stream       = $pipes[1];
    stream_set_blocking($stream, 0);
    
    //Call stream_select with a 10 second timeout.
    $read = array($stream); $write = NULL; $except = NULL;
    while (!feof($stream) && !connection_aborted()
            && stream_select($read, $write, $except, 10)) {
    
        //Print out all the lines we can.
        while (($buffer = fgets($stream)) !== FALSE) {
            echo 'data: ' . $buffer . "
    ";
            @ob_flush();
            flush();
        }
    
    }
    
    //Clean up.
    fclose($stream);
    $status = proc_get_status($process);
    if ($status !== FALSE && $status['running'] === TRUE)
        proc_terminate($process);
    proc_close($process);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题