Php脚本内存不足
嗨,我正在尝试编写一个函数,它将从远程服务器导入预告片 代码将我们magento商店中的产品与拖车服务器上的拖车相匹配。 问题是脚本占用了所有内存。有没有任何方法可以优化它以避免占用内存 这是我的功能Php脚本内存不足,php,magento,Php,Magento,嗨,我正在尝试编写一个函数,它将从远程服务器导入预告片 代码将我们magento商店中的产品与拖车服务器上的拖车相匹配。 问题是脚本占用了所有内存。有没有任何方法可以优化它以避免占用内存 这是我的功能 function getTrailers() { echo "<pre>\n"; $this->_acquireLock(); $_productCollection = Mage::getModel('catalog/product')-&g
function getTrailers() {
echo "<pre>\n";
$this->_acquireLock();
$_productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('attribute_set_id', array('eq' => '9'));
//->addAttributeToFilter('Sku', array('eq' => '843594'));
$this->_logLine(sprintf("Starting import of Trailers" ));
$i=0;
$server = "http://trailer.server.com/trailers";
foreach($_productCollection as $product) {
$thispro = Mage::getModel('catalog/product')->load($product->getId());
$attributeValue = $thispro->getFaktaId();
//echo memory_get_usage() . "<br>\n";
//echo memory_get_peak_usage() . "<br>\n";
if($thispro->getMainTrailer()=="") {
if($attributeValue != "") {
$im = $attributeValue.".mp4";
$url = $server.$im;
$exist = $this->file_exists_remote($url);
if($exist) {
$i++;
$product->setMainTrailer($url);
$product->save();
} else {
}
}
}
}
$this->_logLine(sprintf("Imported %d Trailers...", $i));
$this->_releaseLock();
echo "</pre>\n";
函数getTrailers(){
回音“\n”;
$this->_acquireLock();
$\u productCollection=Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('attribute_set_id',array('eq'=>'9');
//->addAttributeToFilter('Sku',数组('eq'=>'843594');
$this->_logLine(sprintf(“开始进口拖车”);
$i=0;
$server=”http://trailer.server.com/trailers";
foreach($\u productCollection作为$product){
$thispro=Mage::getModel('catalog/product')->load($product->getId());
$attributeValue=$thispro->getFaktaId();
//回显内存\u获取\u用法()。“\n”; //回显内存获取峰值使用率()。“
\n”; 如果($thispro->GetMainTraile()==“”){ 如果($attributeValue!=“”){ $im=$attributeValue。“.mp4”; $url=$server.$im; $exist=$this->file\u exists\u remote($url); 如果($存在){ $i++; $product->SetMainTrail($url); $product->save(); }否则{ } } } } $this->_logLine(sprintf(“导入的%d个拖车…”,$i)); $this->_releaseLock(); 回音“\n”;
}下面的文章介绍了防止php内存不足的不同方法
美元productCollection有多大?您可以尝试分批获取产品,或者在for循环结束时从集合中删除产品。您可以在循环中重用相同的模型实例:
$_productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
->addAttributeToSelect(array('fakta_id', 'main_trailer'));
或者最好只在收藏中装入你需要的东西
foreach($_productCollection as $product)
{
$attributeValue = $thispro->getFaktaId();
// ...
}
然后在无需重新加载产品的情况下循环:
function getIds() {
return Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
->getAllIds();
}
...
foreach(getIds() as $id) {
$thispro = Mage::getModel('catalog/product')->load($id);
...
// Further you have to replace all $product occurences with $thispro
}
你的主要问题是你收集了一些你从未使用过的产品。实际上你不需要这些产品。你只需要他们的身份证 因此,您必须按如下方式编辑代码:
Witch metod可用于在每个循环结束时删除每个产品它将内存使用量从9855568更改为6885536,但我认为我需要比第一步更高的内存使用量10MB内存使用量对很多Magento产品都不起作用。您应该将memory_limit php配置变量增加到512MB左右
function getIds() {
return Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
->getAllIds();
}
...
foreach(getIds() as $id) {
$thispro = Mage::getModel('catalog/product')->load($id);
...
// Further you have to replace all $product occurences with $thispro
}