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

PHP method to prevent high CPU usage in while true loop

不言
Release: 2023-03-23 13:30:01
Original
2448 people have browsed it

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;
}
Copy after login


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



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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template