gpt4 book ai didi

php - 为什么等待运行时的进程数很高,但我的 CPU 却有 99% 空闲?

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:46 24 4
gpt4 key购买 nike

我有一个脚本可以同时运行大量(大约 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 个 procs r,但是它更多(见下文)。当我运行上面的脚本并立即触发 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

当系统处于“休息”状态时,我得到以下输出:

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

最佳答案

我不确定,但我看到三种可能性:

  1. 您的 sh 脚本在发送其他命令之前等待较早的命令处理
  2. 可能是一些 CURL 问题。可能它一次只能处理一定数量的查询?
  3. Apache 无法一次处理所有请求,所以他们必须等待

您可以通过查看topps auxw 命令输出、apache 日志以及它们是如何执行的来获得一些想法。此外,您可以在每个 curl 命令后添加一些示例输出,以查看它们是否一次性运行。如果它们确实同时运行,则为网络服务器问题。

关于php - 为什么等待运行时的进程数很高,但我的 CPU 却有 99% 空闲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com