Home  >  Article  >  Backend Development  >  PHP method to prevent high CPU usage in while true loop

PHP method to prevent high CPU usage in while true loop

不言
不言Original
2018-04-10 10:49:002418browse

The content of this article is about how to prevent excessive CPU usage in while true loops in PHP. Friends in need can refer to

A simple example

For example It can be used when using PHP and redis for message queues. It can be modified according to the actual application scenario

test.php
<?php
$i = 0;
while (true) {
    $i++;
    if ( $i%30==0 ){
        sleep(1);
    }
    echo $i;
}


<?php
$i = 0;
while (true) {
    $i++;
    if ( $i%30==0 ){
        sleep(1);
    }
    echo $i;
}



Execution: php test.php

The above is the detailed content of PHP method to prevent high CPU usage in while true loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn