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条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值