Answer the question
In order to leave comments, you need to log in
How to reduce CPU load when parsing xml?
I download files weighing from 30 to 700mb, there is often a large load on the processor, how can I solve this problem?
I use extension from Hobnob\XmlStreamReader\Parser for yii2
$parser = new \Hobnob\XmlStreamReader\Parser();
$parser->registerCallback(
'/source/products/product',
function( \Hobnob\XmlStreamReader\Parser $parser, \SimpleXMLElement $node ) {
parseListing($node);
}
);
function parseListing($product)
{
if (empty($product))
return;
$data = xml2array($product);
/*тут уже сохраняю к себе в базу данные из $data*/
}
function xml2array($xml)
{
$arr = array();
foreach ($xml->children() as $r)
{
/** @var \SimpleXMLElement $r */
if (count($r->children()) == 0)
$arr[$r->getName()] = strval($r);
else
$arr[$r->getName()][] = xml2array($r);
}
return $arr;
}
Answer the question
In order to leave comments, you need to log in
most likely it will not be possible to seriously reduce the load, the xml is very redundant and heavy, and given the weight of the files that you brought, this should really load the most. Alternatively, if this is not a priority task, running it in the console with a low priority will be slower, but the processor will not be so loaded at the peak.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question