Comparison between Swoole coroutine and traditional fpm synchronization mode

藏色散人
Release: 2023-04-06 12:28:02
forward
3530 people have browsed it

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 ofphp-fpm/apachewait.

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

Execution results:

0 1 2 3
Copy after login

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

Execution results:

time: 1.0326268672943s time: 0.10734605789185s
Copy after login

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!

Related labels:
source:yurunsoft.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!