我必须每天运行一个 php 脚本,代码是这样的:
<?php
// SET PROCESS PRIORITY
// SETTING UP THE LOG FILE
$ob_file = fopen('sync.log','w');
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');
// GET PARAMETERS
$lastid=$_GET['lastid'];
if ($lastid && !is_numeric($lastid)){
die("Loser");
}
// SERVER ROOT CONFIGURATION
$serverpath = 'http://dev.xxx.com/ops/';
// FACEBOOK SDK
require '../classes/facebook.php';
require '../classes/fbconfig.php';
// MYSQL CONFIG
include('../dbconfig.php');
// OPEN DATABASE CONNECTION $CON
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname, $con);
// GET LAST ID FROM DATABASE AND COUNT TO ZERO
// ONLY 10 PER TIMES
$sql = "SELECT * FROM ops_pictures
ORDER BY id DESC
LIMIT 10;";
$query = mysql_query($sql,$con);
while($row = mysql_fetch_array($query)) {
if (!$lastid){
$lastid = $row['id'];
}
}
//START COUNTING
for ($i = $lastid; $i >= 0 ; $i --)
{
// set_time_limit(); // RESET TIMEOUT TO GO FOREVER
echo $i .' - ';
// LOAD RECORDS FROM FACEBOOK AND DISPLAY THE PHOTO URL
// Get User ID
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,user_likes,read_stream,publish_stream,user_about_me,user_photos'
)
);
};
// FQL QUERY FOR THE PICTURE
$actualurl = $serverpath . 'photo.php?pid=' . $i;
//echo $actualurl;
try {
$fql = 'SELECT url, normalized_url, share_count, like_count, comment_count, total_count,
commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url="'.$actualurl.'"';
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
// FQL queries return the results in an array, so we have
// to get the user's name from the first element in the array.
$linkstat = $ret_obj[0];
$theurl = $linkstat['url'];
$sharecount = $linkstat['share_count'];
$likecount = $linkstat['like_count'];
$commentcount = $linkstat['comment_count'];
$totalcount = $linkstat['total_count'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
// PUT THE RECORDS IN
$sql = "UPDATE ops_pictures SET likecount='$likecount', sharecount='$sharecount', totalcount ='$totalcount'
WHERE id='$i'";
mysql_query($sql,$con);
//END COUNT
}
// CLOSE DATABASE
mysql_close($con);
ob_end_flush();
?>
基本上这是一个循环,我网站上的每张图片都从 Facebook 获取 FQL 查询,并将结果存储在我的 MYSQL 数据库中。我什至记录了完成的文件。
问题: - 如果我从浏览器调用它,它会锁定服务器,我尝试加载的任何 php 文件将永远等待并给出 500 服务器错误。 - 我不希望它超时,因为我希望它循环数据库中的所有图片并使用新值刷新它们
决议 - Cron Job(这应该允许脚本在“后台”模式下运行吧? - 将脚本分成不同的部分,每次处理 10 张图片(不好) - 在代码之前使用 proc_nice() 为整个 php 指令分配较低的优先级。
实际上我有这个脚本使服务器过载并且任何东西都变得不可用,你怎么看?
非常感谢!!
最佳答案
在您的服务器上运行任务管理器或 top,查看它是否正在占用您的 CPU 或内存或两者。
如果它影响了您的内存,请使用 unset 并随时清理您的变量,并且在每个循环中不再需要它们。 http://php.net/manual/en/function.unset.php
如果您遇到 CPU 使用问题,如果您使用的是 unix 或我相信 Windows 中的任务管理器,则可以使用 nice。 Can I throttle the max CPU usage of a php script?
另一个瓶颈可能是硬盘吞吐量,但我怀疑这是您的问题。
该程序将需要很长时间才能运行,但最终会完成。
您还可以尝试优化您的代码并尽可能多地重用,使用引用而不是仅仅赋值,因为这会使复制的内存使用量增加一倍。
$myvar = &$other_var['var'];
一定要设置你的内存使用率高,像这样:
ini_set('memory_limit', '700M');
并像上面那样将超时设置为永远:
set_time_limit (0);
关于PHP 脚本使服务器过载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476773/