Home  >  Article  >  Backend Development  >  Comparison between Swoole coroutine and traditional fpm synchronization mode

Comparison between Swoole coroutine and traditional fpm synchronization mode

藏色散人
藏色散人forward
2019-04-28 10:07:103626browse

If arrays are the essence of PHP, if you can't play with arrays, you can't be considered to be able to use PHP at all. The same applies to coroutines for Swoole. If you don’t understand coroutines and use Swoole, you are using them blindly.

Comparison between Swoole coroutine and traditional fpm synchronization mode

First of all, Swoole can only run in command line (Cli) mode, so we use the command line for development and debugging instead of php-fpm/apache wait.

In Swoole, we can use `\Swoole\Coroutine::create()` to create a coroutine, or you can also use the abbreviation `go()`.

First introduction to Swoole coroutine

go(function(){
    go(function(){
        echo 0, PHP_EOL;
    });
    echo 1, PHP_EOL;
});
go(function(){
    echo 2, PHP_EOL;
});
go(function(){
    echo 3, PHP_EOL;
});

Execution results:

0
1
2
3

Comparison between Swoole coroutine and synchronization mode

We have been saying that Swoole coroutine is suitable for I/O-intensive scenarios. Under the same hardware configuration environment, it will carry more access than the traditional synchronous mode.

The file reading and writing and network communication requests (MySQL, Redis, HTTP, etc.) we are familiar with are all I/O-intensive scenarios.

Assume that a SQL query takes 100ms. In traditional synchronization mode, the current process cannot perform other operations during this 100ms time. If you want to execute this SQL ten times, it may take more than 1 second.

If you use coroutines, although different coroutines are executed in order, during the previous waiting period of 100ms, the bottom layer will schedule the CPU to perform the operations of other coroutines. In other words, it is possible that before the first query returns results, several other queries have been sent to MySQL and are being executed. If ten coroutines are opened and this SQL is executed separately, it may only take 100 ms to complete.

The test code is as follows:

Swoole\Runtime::enableCoroutine(); // 开启一键协程化
 
function work()
{
    $pdo = new \PDO('mysql:host=127.0.0.1;dbname=db_test', 'root', 'root');
    $pdo->exec('select SLEEP(0.1)'); // 模拟sql需要执行 100ms 的情况
}
$time = microtime(true);
for($i = 0; $i < 10; ++$i)
{
    work();
}
echo &#39;time: &#39;, (microtime(true) - $time), &#39;s&#39;, PHP_EOL;
$time = microtime(true);
for($i = 0; $i < 10; ++$i)
{
    go(&#39;work&#39;);
}
swoole_event_wait(); // 等待所有协程执行完
echo &#39;time: &#39;, (microtime(true) - $time), &#39;s&#39;, PHP_EOL;

Execution results:

time: 1.0326268672943s
time: 0.10734605789185s

The above code can be imagined as the time required for a single process to process 10 requests. Each request requires executing a SQL statement that takes 100ms.

Synchronous mode, fpm takes about 1s. It can be seen that nothing can be done while waiting for 100ms.

Coroutine model, the one that takes about 0.1s is Swoole. During the waiting period of 100ms, the current coroutine will be suspended, and the underlying scheduling will let the CPU perform the operations of other coroutines.

The above is the detailed content of Comparison between Swoole coroutine and traditional fpm synchronization mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yurunsoft.com. If there is any infringement, please contact admin@php.cn delete