我有一个脚本,可以同时运行大量(大约100个)php文件。我注意到每个脚本的开始时间经常被推迟很多。当一次启动所有脚本时,有些脚本只会在15秒后启动。
我使用下面的代码来同时运行这些脚本(这段代码工作没有任何错误)。请注意,这100个php文件与同时调用它们的脚本位于同一服务器上。
function my_curl ($i, $url, $post_data, $return, $auth='', $proxy='', $timeout=5) {
$curl="/usr/bin/curl -s";
if ($post_data!='') $curl.=" --data '$post_data'";
if ($timeout!='') $curl.=" --connect-timeout $timeout";
if ($auth!='') {
list($user,$pass)=explode(':',$auth);
$curl.=" --basic --user $user:$pass";
}
if ($proxy!='') {
list($proxy_ip,$proxy_port,$proxy_user,$proxy_pass)=explode(':',$proxy);
$curl.=" --proxy $proxy_ip:$proxy_port --proxy-user $proxy_user:$proxy_pass";
}
if ($return==0) {
$curl.=" $url >/dev/null 2>/dev/null &";
} else {
$curl.=" $url >return-$i 2>/dev/null &";
}
return("$curl'n");
}
$to_run='';
for ($i=0; $i<$max; $i++) {
$url='http://www.some_url_to_the_same_server.com/post.php';
$post_data="var=web-$i";
$to_run.=my_curl($i, $url, $post_data, $return, $auth, $proxy, $timeout);
}
file_put_contents(realpath($_SERVER["DOCUMENT_ROOT"]).'/run_data/runner.sh',$to_run);
shell_exec('/bin/bash '.realpath($_SERVER["DOCUMENT_ROOT"]).'/run_data/runner.sh');
我有一个3CPU linux centos服务器。我知道我的系统不应该有超过12个进程,但实际上要多得多(见下文)。当我运行上面的脚本并一次触发100个php脚本时,我看到下面的vmstat.
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
30 0 60 90388 14428 223996 0 0 20 10 3 10 0 0 99 1 0
当系统处于"rest"状态时,我得到以下输出:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 60 155064 14672 223952 0 0 20 10 3 10 0 0 99 1 0
由于procs r显示有CPU过载,我不明白为什么CPU显示99%空闲而procs r同时非常高。
我能做些什么来提高系统的性能,使所有100个脚本一次被触发?
非常感谢你的帮助。
更新1:这是我的httpd.conf文件的相关部分:
User apache
Group apache
KeepAlive On
KeepAliveTimeout 30
ServerAdmin admin@localhost
DocumentRoot "/var/www/html"
MaxClients 50
MaxRequestsPerChild 50
StartServers 5
MinSpareServers 5
MaxSpareServers 20
MaxKeepAliveRequests 50
我不确定,但我认为有三种可能性:
- 您的
sh
脚本在发送其他 之前等待先前的命令处理。 - 可能是
CURL
问题。也许它一次只能处理一定数量的查询? - Apache不能一次处理所有的请求,所以他们必须等待
您可以通过查看top
或ps auxw
命令输出,apache
日志并查看它们是如何执行的来了解一些想法。此外,您可以在每个curl
命令之后添加一些示例输出,以查看它们是否在一次运行。
对于这些curl调用,您向服务器发出单独的HTTP请求。当MaxClients
指令设置为50
时,一次只能处理这么多请求。
可能的解决方案:
1)如果你不需要过程输出,不要等待:
pclose(popen('/path/to/executable', 'r'));
2)如果不是绝对必要的话,不要使用子请求-尝试将其重写为CGI脚本,可以直接运行,而不是进行子请求。
3)增加apache conf中同时处理的最大连接数:
MaxClients 256