How to "bypass" the Maximum Execution time of 30 Seconds exceeded in php?

Asked

Viewed 847 times

-1

I have a script that imports the properties of a real estate by an xml.

When there are many properties, it gives timeout after 30 seconds (Maximum Execution time of 30 Seconds exceeded).

Is there any way around this? Below my shorthand code and an alternative that I tried but failed.

$xml = simplexml_load_file("/arquivo.xml");
foreach($xml->children()->children() as $i){
    //PEGA OS DADDOS DO XML
    //PEGA FOTOS E REDIMENSIONA
    //INSERE NO BANCO DE DADOS
}

Then I tried to get him to upgrade from one to one, updating the page.

He first inserts all the properties into a database called "IMPORT". Then take the last one, pull the data imports and deletes from that bank, and so on until you finish.

//FAZ CONSULTA MYSQL BUSCA QUAIS IMOVEIS PARA IMPORTAR PEGANDO APENAS O ULTIMO
$xml = simplexml_load_file("/arquivo.xml");
foreach($xml->children()->children() as $i){
    if($i->id_xml==$id_consulta){
        //PEGA OS DADDOS DO XML
        //PEGA FOTOS E REDIMENSIONA
        //INSERE NO BANCO DE DADOS

        //EXCLUI ESSE IMÓVEL DO BANCO DE DADOS "IMPORTAR"
    }
}

That way he stayed longer, but after a while he also hangs.

  • Take a look https://www.php.net/manual/en/function.set-time-limit.php

  • What takes the most time on the tasks you are performing? Have you tried to evaluate which part it takes the most to isolate it? My guess is to download and resize the images. You can take an out-of-flow approach to running this process through a queue or messaging.

  • Well, I understand that you update the page every time you enter it. Correct? If so, I recommend block insertion. How’s your script’s rule for Insert?

  • <code>&#xA;protected function inserir(array $ins):&#xA; {&#xA; try {&#xA; $col = implode(", ", array_keys($ins));&#xA; $val = ":" . implode(", :", array_keys($ins));&#xA; $stmt = Connect::getInstance()->prepare("INSERT INTO {$this->entity} ({$col}) VALUES ({$val})");&#xA; $stmt->execute($this->filter($ins));&#xA; return Connect::getInstance()->lastInsertId();&#xA; } catch (\PDOException $exception) {&#xA; $this->fail = $exception;&#xA; return null;&#xA; }&#xA; }&#xA;</code>

2 answers

-1

Hello, you can change this variable by going to the file php.ini and looking for max_execution_time;

Example:

max_execution_time = 60 # o tempo será alterado para 60 segundos.
  • 1

    But what if he needs to handle another file with twice as much real estate? Complexity is expected to be at least linear, so if the input is doubled, at least the processing time is doubled

-2

Good morning! Don’t like working with arrays I particularly like it and believe it would be an option

$xml = simplexml_load_file('integracao.xml');
$xmlArray = json_encode($xml, true);
$xmlArray = json_decode($xmlArray, true);

/*
Depois que estiver com o conteudo do xml em um array
Nesse momento usamos a implementação do php ArrayObject que tem inumeros 
recursos para trabalhar com array e melhorando a performance
*/

$arrayobject = new ArrayObject($xmlArray);
/*
Aqui nesseo ponto como estamos usando ArrayObject podemos usar o recurso de 
iterator
*/
$iterator = $arrayobject->getIterator();

print "<pre>";
while ($iterator->valid()) {
  echo $iterator->key() . ' = ' . print_r($iterator->current()) . "\n";
  $iterator->next();
}

And then while will work like foreach where iterator->key is the key and iterator->Current is the content

This process helps in performace and memory may be an option

  • convert to array will ensure that no timeout happens?

  • not! I just thought of an alternative to try to reduce processing time. because I did some tests and apparently processed faster.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.