PHP cli来内存使用的优化

问题描述:

我试图code鱿鱼定制url_rewriter。
&安培;也使用类似squidGuard的一些其他url_rewriter程序
所以必须使用一个包装能够同时使用或任何其他程序。

I am trying to code a custom url_rewriter for squid. & also with using some other url_rewriter programs like squidGuard so have to use a wrapper to able use both or any other program.

当我尝试用PHP循环。
(这是鱿鱼如何与外部程序的通信方式。
标准输入/输出。它给你一个网址&安培;你必须发送新一或旧一回。 )

when i try to loop with php. (that's the way how squid communicates with external programs. STDIN/STDOUT. it gives you a url & you have to send the new one or old one back. )

它有一个毁灭性的内存使用情况,甚至无所作为。
我已经改变了与另一个bash脚本是只有几行来包装它。
&安培;它循环,而不是PHP。 PHP的调用外部。当PHP脚本与URL来完成其返回&安培;退出。
这种方式比,而不是循环的PHP脚本更加好。

it has a devastating memory usage even doing nothing. i've changed to wrap it with another bash script it is only a few lines. & it loops instead of php. calls php externally. when php script is done with the url returns it & exits. this way is much more better than instead of to loop the php script.

在PHP脚本中几乎没有什么了。 (怎么我现在还在开发它。)
它只是更换video.yahoo.com到youtube.com。
和几个变量和爆炸()来解析输入字符串这一切......
但脚本仍然在使用的内存量巨大

in php script nearly there is nothing now. (coz i'm still developing it.) it's only replacing video.yahoo.com to youtube.com. and a few variable and an explode() to parse input string that's all... but script is still using huge amount of memory


在这里是顶级的输出:

 PID USER   VIRT  RES  SHR S %CPU %MEM TIME+  COMMAND

32059 squid   19720 7432 4396 R    0.9    2.9    0:00.02   php      
32063 squid   19720 7436 4396 R    0.9    2.9    0:00.02   php      
32066 squid   19720 7436 4396 R    0.9    2.9    0:00.02   php      
32068 squid   19460 6188 3472 R    0.9    2.4    0:00.02   php      
32070 squid   19720 7432 4396 R    0.9    2.9    0:00.02   php      
32074 squid   19588 6792 3924 R    0.9    2.6    0:00.02   php      
32077 squid   19720 7436 4396 R    0.9    2.9    0:00.02   php



这里是PHP脚本

#!/opt/lampp/bin/php -q 
<php
ini_set('html_errors',false);
ini_set('implicit_flush',true);
ini_set('max_execution_time',0);
ini_set('register_argc_argv',true);

$nl="\n"; $tab="\t";
$ds=DIRECTORY_SEPARATOR;
$lamppdir='/opt/lampp/';
$htdocsdir='/opt/lampp/htdocs/';
$wdir='/opt/lampp/htdocs/bin/';
$incdir=$htdocsdir.'inc/';
$logfile=$wdir.'log.txt';

if ($argc>1){
    $return=$argv[1];
    $return=explode(' ',trim($return));
    $url=$return[0];
    $sourceip=$return[1];
    $user=$return[2];
    $method=$return[3];
    $urlgroup=$return[4];
    $myip=$return[5];
    $myport=$return[6];

    $logdata=$argv[1];

    // if(strlen($logdata)>50){ file_put_contents($logfile,$logdata.$nl,FILE_APPEND); }

    fwrite(STDOUT,$return[0]."\r\n");
}

exit(0);

这里是bash脚本

And here is the bash script

#!/bin/bash
 lamppdir=/opt/lampp/
 phpexecpath=/opt/lampp/bin/php
 phpredirectorpath=/opt/lampp/htdocs/bin/redir.php
 logdfile=/opt/lampp/htdocs/bin/log.txt
 forcedexit=false

 while [ "${forcedexit}" != "true" ]
 do
    read squidinput
    phpout=`"${phpexecpath}" "${phpredirectorpath}" "${squidinput}"`
    echo "${phpout}"
 done

 echo "\r\n"

exit 0



我已经用Google搜索来查找有关PHP CLI和放任何有用的文件;源的使用,但没有运气。

i already googled to find any useful documentation about PHP cli & source usage but no luck.

你有什么建议,以减少源的使用?

Do you have any advice to decrease source usage ?

我敢打赌,你会在这个笑。我一直在寻找在错误的地方。

i bet you will laugh at this. i was looking at the wrong place.

鱿鱼长跟踪标准输入/输出之后。
我刚刚在环路中加入一个if语句。控制字符串的长度为URL。

after a long tracing stdin/stdout of squid. i've just added an if statement in the loop. to control string's length as a url.

while [ "${forcedexit}" != "true" ]
do
    read squidinput
    if [ -n squidinput ]
       then
    	phpout=`"${phpexecpath}" "${phpredirectorpath}" "${squidinput}"`
    	echo "${phpout}"
    fi
done

结果:有背景没有等待PHP进程了。
因为它的操控性和放大器;只是以毫秒为单位退出。

result: there is no waiting php processes in background anymore. because it's handling & exiting just in milliseconds.

无IF语句鱿鱼发送空格和换行脚本所以它永远不会停止。我被修剪用PHP这就是为什么我不能得到早期鱿鱼的怪异stdins输入字符串。修剪$ argv的仅仅是带来了习惯。
鱿鱼的版本是2.6stable7可能是它在早期版本都一样。
我刚刚失去了半天:(
谢谢大家阅读。

without the IF statement squid is sending empty spaces and newlines to script so it never stops. i was trimming input string with php that is why i couldn't get earlier squid's weird stdins. trimming $argv is just an habit brought. squid version was 2.6stable7 probably it's all the same in earlier versions. i've just lost half a day :( thank you for everyone read.