2012-07-13 35 views
3

我必须每天运行PHP脚本,代码是这样的: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.danielepiccone.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作业运行(这应该允许脚本在“后台”运行模式右 - 斯普利特的不同零件加工每单位时间(不好)10张图片 脚本 - 使用proc_nice()的代码之前低优先级分配给整个PHP指令

其实我有这个脚本超载服务器和任何东西无法使用,你怎么看?

谢谢很多!

+0

什么'回声$ lastid'打印? – 2012-07-13 19:07:15

+0

在数据库中记录文件不容易吗? – 2012-07-13 19:10:20

+0

服务器可能会做很多IO,运行'iotop'来确认。另一个可能是更新语法,如果没有事务处理,这是很痛苦的。 – Pentium10 2012-07-13 19:10:31

回答

1

在您的服务器上运行任务管理器或顶部,查看它是否击中了您的CPU或您的内存或两者。

如果它正在击中你的记忆,那么使用取消设置并清理你的变量,并且不再需要它们,通过每个循环。 http://php.net/manual/en/function.unset.php

如果您遇到CPU使用率问题,您可以使用nice,如果您在unix或任务管理器,我相信在Windows中有类似的东西。 Can I throttle the max CPU usage of a php script?

另一个瓶颈可能是硬盘吞吐量,但我怀疑这是你的问题。

程序运行需要很长时间,但最终会结束。

您也可以尝试优化您的代码并尽可能重用,使用引用而不是仅仅指定,因为这会使内存使用量翻倍。

$myvar = &$other_var['var']; 

请务必设置您的内存使用量高,是这样的:

ini_set('memory_limit', '700M'); 

并设置您超时永远像你有以上:

set_time_limit (0); 
+1

谢谢!真的很全面! – dpi 2012-07-14 06:34:34

+0

不客气:) – Wolfe 2012-07-14 18:52:13

+0

你也可以将所有的变量外循环声明的全局变量,所以内存是简单地重复使用,这将节省CPU和内存时,未设置是昂贵的操作。 – Wolfe 2012-07-14 21:42:27