2016-03-09 83 views
-2

我需要运行一个cron,基本上就会生成MPDF一个PDF文件,并将其发送到指定的人,电子邮件的这个cron将会处理量可从1人什么到1000人,甚至更多。处理大量的数据用的cronjob

生成过程已经花费相当多的时间,因为需要的cron访问数据库,并准备PDF文件。

此刻,当cron的达到10位客户就弹了,因为已达到内存限制。(这需要大量的内存)

反正是有运行cron和已将其释放内存,或者甚至没有内存限制运行?任何可能的解决方案......

代码

$table = "tblclients"; 
$fields = "id"; 
$where = array("status" => "Active"); 
$clientsresult = select_query($table,$fields,$where); 
while ($clientsdata = mysql_fetch_array($clientsresult)){ 
    $client_id=$clientsdata['id']; 
    $period_from=date("d-m-Y", strtotime(date("Y-m-d").' -0 years -1 months -0 days')); 
    $period_to=date(sprintf("%02d", $day).'-m-Y'); 
    if(get_balance_for_period($client_id,-2000,29)>0){ 
     $statement=generate_custom_report($client_id,$period_from,$period_to,"F"); 
     mail_statment($client_id,MYROOT.'/generated/'.$statement,$period_from,$period_to); 
     unlink(MYROOT.'/generated/'.$statement); 
    } 
} 

下面的代码显示了cron是如何运行的,generate_custom_report生成报告,然后使用mail_statement函数发送,然后将PDF格式被删除。 ..

我不知道哪里的内存将被保留,因为相同的变量在每个使用看,是内存没有释放?

内存使用:

在下面的例子中,你应该能够看到内存的使用方式与每一个客户。你会注意到在这里只处理了25个客户。但内存使用600MB是...这意味着,如果你的过程可以说,1000你将最终需要24GB的内存,其中大部分服务器将不提供...

是否有可能解决内存滥用?

sending to 1 | Memory bytes: 16123384 
sending to 12 | Memory bytes: 56914024 
sending to 13 | Memory bytes: 79047568 
sending to 14 | Memory bytes: 111560288 
sending to 17 | Memory bytes: 222810720 
sending to 23 | Memory bytes: 250151664 
sending to 24 | Memory bytes: 293685600 
sending to 33 | Memory bytes: 299987024 
sending to 30 | Memory bytes: 313091856 
sending to 34 | Memory bytes: 386661752 
sending to 41 | Memory bytes: 411917048 
sending to 52 | Memory bytes: 446857024 
sending to 65 | Memory bytes: 457177864 
sending to 59 | Memory bytes: 464880584 
sending to 63 | Memory bytes: 498235608 
sending to 97 | Memory bytes: 526843496 
sending to 83 | Memory bytes: 534199208 
sending to 86 | Memory bytes: 546184000 
sending to 85 | Memory bytes: 556632648 
sending to 88 | Memory bytes: 567034592 
sending to 89 | Memory bytes: 576347920 
sending to 94 | Memory bytes: 589466432 
sending to 93 | Memory bytes: 598519680 
sending to 96 | Memory bytes: 602776992 
sending to 102 | Memory bytes: 604044912 
+1

一般情况:当然,你可以这样做。你只需要以一种不会试图在内存中保存太多东西(变量中的数据)的方式编写你的脚本。如果你运气不好,并且mPDF编码不好,所以它*不会释放内存,你可能会遇到一些问题。当然,如果没有看到你正在做什么,我们就不知道这些。 – deceze

+0

喜@deceze请检查更新 – Marcel

回答

0

尝试在PHP文件中添加以下行,并让我知道它是否有效。

ini_set("max_execution_time", "0"); 
ini_set("memory_limit","2048M"); // change it as required 
+0

的事情是,我不知道用什么内存会。在我的系统上,它运行了80个客户端的2GB内存。但是,在其他系统上它(取决于客户端配置文件,所需的内存可能会更多) – Marcel

+0

@Marcel设置'memory_limit'到'-1'将“删除” memory_limit的。 – Timothy

+0

将memory_limit设置为-1(无限制)。将工作?不知道将它设置为-1是否合适 – sravs