PHP function sleep reference

1

I'm looking for a way to limit the use of a script for a certain time, good as it said in another question I do not want to use events of MySQL.

Searching, searching, trying to do, found this in the documentation, sleep function It says the following:

  

sleep - Delay script execution

It's not well documented in my mind, but down there are examples done by contributors.

How does this function really work?

Note: This question is different from this. Limit quantity by time

    
asked by anonymous 16.11.2017 / 08:25

1 answer

4

sleep , as the documentation itself says, has the purpose of delaying the execution time of the script, from the point it is invoked.

Generally, the sleep function is used in infinite loops, often used in background running on servers, to perform a certain task.

For example, if you need to check from time to time whether something is pending in the database to be able to process this data through a Webservice:

// Esse script está rodando pela linha de comando
// Laço (ou loop) infinito para executar uma verificação repetitiva

while (true) {


    $solicitacoes = Solicitacao::where('pendente', '=', 1)->get();

    foreach ($solicitacoes as $solicitacao) {
         $this->processarWebservice($solicitacao);

         $solicitacao->update(['pendente' => 0]);
    }

    sleep(60); // Determina que a próxima iteração sera feita daqui a 60 segundos
}

It's rare to use it in HTTP, but I've already been able to contemplate an implementation of long polling that used sleep to slow data query time.

    
16.11.2017 / 16:35