


The difference between traditional fpm synchronization mode and swoole coroutine
First of all, Swoole can only run in command line (Cli) mode, so we use the command line for development and debugging, not php-fpm/apache, etc. 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
Execution result:
Swoole coroutine Comparison with synchronous mode
We have always said 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;
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.
Recommended related articles and tutorials: swoole tutorial
The above is the detailed content of The difference between traditional fpm synchronization mode and swoole coroutine. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
