Swoole coroutine comparison with the conventional synchronous mode fpm

If the array is the essence of PHP array 6 does not play, can not be regarded with PHP. That is the same reason coroutine for Swoole, do not understand the coroutine go with Swoole, that is in the blind use.

 

 

 

First, Swoole can only be run from the command line (Cli) mode, so we are developing and debugging using the command line instead  php-fpm/apache and so on.

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

Acquaintance 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;

});

Results of the:

0

1

2

3

Swoole synchronization pattern is compared with coroutine

We have been saying Swoole coroutine suited for I / O intensive scenes, in the same hardware configuration environment, it will carry more traffic than the traditional synchronous mode.

We are familiar with the file read and write, network communication request (MySQL, Redis, Http, etc.) belong to the I / O-intensive scenes.

Assuming that a SQL query is 100ms, in the traditional synchronous mode, the current process 100ms at this time, can not do other operations. If you are performing ten times this SQL, you may need to spend more than 1s.

And if during the coroutine, although different coroutine is executed sequentially, but a previous waiting 100ms, the CPU dispatches the bottom, to perform other operations coroutine. In other words, the first query may not return results, several other inquiries have been sent to the MySQL and are being implemented in the. If enabled ten coroutine, respectively, the SQL execution this, you may only need to spend 100 + ms to complete.

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 'time: ', (microtime(true) - $time), 's', PHP_EOL;

$time = microtime(true);

for($i = 0; $i < 10; ++$i)

{

    go('work');

}

swoole_event_wait(); // 等待所有协程执行完

echo 'time: ', (microtime(true) - $time), 's', PHP_EOL;

Results of the:

time: 1.0326268672943s

time: 0.10734605789185s

The above code can be assumed to, single-process 10 the processing time required for the request. Each request takes 100ms need to perform a SQL statement.

Synchronous mode, consuming about 1s is fpm. As can be seen, while waiting 100ms can not do anything.

Coroutine model, cost is about 0.1s Swoole. During this waiting 100ms hang coroutine, lowest scheduling allow CPU to perform other operations coroutine.

Details of the above synchronization pattern is compared with the conventional fpm Swoole coroutine

Above hope to help everyone, many PHPer always encounter some problems and bottlenecks in the advanced time, write more business code no sense of direction, I do not know from where to start to ascend, which I compiled some information, including but not limited to: a distributed architecture, highly scalable, high-performance, high-concurrency, server performance tuning, TP6, laravel, YII2, Redis , Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, micro-services, Nginx, etc. more advanced knowledge required for advanced dry goods can be free for everyone to share, I need to join the official group , click here .

Recommended reading:

Yaezakura: Go with the difference Swoole coroutine coroutine, very detailed, very fast hardware

Guess you like

Origin www.cnblogs.com/a609251438/p/12078363.html