函数 checkServer($domain, $port=80) { 全局 $checkTimeout, $testServer;
$status = 0;
$starttime = microtime(true);
$file = @fsockopen ($domain, $port, $errno, $errstr, $checkTimeout);
$stoptime = microtime(true);
if($file)
{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
else
{
$testfile = @fsockopen ($testServer, 80, $errno, $errstr, $checkTimeout);
if($testfile)
{
fclose($testfile);
$status = -1;
}
else
{
$status = -2;
}
}
return $status;
}
测试服务器是 google.sk,checkTimeout 是 10 秒。这确实有效,但是当我尝试在循环中运行它大约 50 次并执行其他操作(mysql 查询和类似的操作)时,它并不慢,但它会导致我的 CPU 100% 负载,直到脚本结束。这是一个让我的 cpu 发疯的 apache 进程……所以我想问你是否对此有任何想法。也许一些提示如何在 python 或 bash 中做同样的事情将不胜感激。
感谢您的回复 :)