Comparison of PHP-FPM and Swoole
I saw a few articles about swoole a few days ago, and I’m here to join in the fun today. My level is limited and my understanding of details may not be in place. I welcome everyone to help me add and correct.
PHP-FPM
Early versions of PHP did not have a built-in WEB server, but provided SAPI (Server API) for third-party connection. The now very popular php-fpm handles the communication between PHP and third-party WEB servers through the FastCGI protocol. For example, the combination of Nginx php-fpm, fpm running in this way is Master/Worker mode, starting a Master process to monitor requests from Nginx, and then forking multiple Worker processes to handle the requests. Each Worker process can only handle one request. The life cycle of a single process is roughly as follows:
1. Initialization module.
2. Initialization request. The request here means requesting PHP to execute code, not an HTTP request.
3. Execute PHP script.
4. End the request.
5. Close the module.
The multi-process model relies on the number of processes to solve concurrency problems. One process can only handle one connection. When a large number of processes are started, process scheduling consumption may account for dozens or even 100% of the CPU, such as the C10K problem. , the multi-process model is insufficient.
Swoole
Swoole also adopts the Master/Worker mode. The difference is that the Master process has multiple Reactor threads. The Master is just an event generator, responsible for monitoring Socket handles. changes in events. Worker runs in a multi-process manner, receives requests from Reactor threads, and executes callback functions (written in PHP). The process of starting the Master process is roughly:
1. Initialize the module.
2. Initialization request. Because swoole needs to be run through the cli, PHP's global variables, such as $_SERVER, $_POST, $_GET, etc., will not be initialized when the request is initialized.
3. Execute PHP script. Including lexical and syntactic analysis, initialization of variables, functions, classes, etc., the Master enters the listening state and will not end the process.
The principle of Swoole acceleration
● Reactor (epoll’s IO reuse method) is responsible for monitoring event changes of Socket handles to solve high concurrency problems.
● Save time on PHP code initialization through memory resident. When using bulky frameworks, the acceleration effect of using swoole is very obvious.
Compare the differences
PHP-FPM
● Master main process / Worker multi-process mode.
● Start the Master and listen for requests transmitted from Nginx through the FastCGI protocol.
● Each Worker process corresponds to only one connection, which is used to execute complete PHP code.
● After the PHP code is executed, all the memory occupied will be destroyed, and the next request will require re-initialization and other tedious operations.
● Only for HTTP Server.
Swoole
● Master main process (composed of multiple Reactor threads)/Worker multi-process (or multi-thread) mode
● Start Master , initialize the PHP code, and the Reactor monitors the event changes of the Socket handle.
● The main thread of Reactor is responsible for the balancing of multi-threads, and the Manager process manages multiple Worker processes, including TaskWorker processes.
● Each Worker accepts requests from Reactor and only needs to execute the PHP code in the callback function part.
● The PHP initialization code is only executed once when the Master starts. The Master enters the listening state and does not end the process.
● It can not only be used for HTTP Server, but also can establish TCP connection and WebSocket connection.
The above is mainly for comparison of core operating machines. The differences listed are just these points that come to mind for the time being. If there are any missed points, please feel free to help me add them~
The above is the detailed content of Comparison of PHP-FPM and Swoole. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use php-fpm for high-performance tuning PHP is a very popular server-side scripting language that is widely used to develop web applications and dynamic websites. However, as traffic increases, the performance of your PHP application may suffer. In order to solve this problem, we can use php-fpm (FastCGIProcessManager) for high-performance tuning. This article will introduce how to use php-fpm to improve the performance of PHP applications and provide code examples. one,

How to use PHP-FPM optimization to improve the performance of PrestaShop applications. With the rapid development of the e-commerce industry, PrestaShop has become the e-commerce platform chosen by many merchants. However, as the size of the store increases and the number of visits increases, the PrestaShop application may encounter performance bottlenecks. In order to improve the performance of the PrestaShop application, a common method is to use PHP-FPM to optimize and improve the application's processing capabilities. PHP-FPM (FastCGI

How to Improve the Performance of WooCommerce Applications Using PHP-FPM Optimization Overview WooCommerce is a very popular e-commerce plugin for creating and managing online stores on WordPress websites. However, as your store grows and traffic increases, WooCommerce apps can become slow and unstable. To solve this problem, we can use PHP-FPM to optimize and improve the performance of WooCommerce applications. What is PHP-FP

Overview of using php-fpm connection pool to improve database access performance: In web development, database access is one of the most frequent and time-consuming operations. The traditional method is to create a new database connection for each database operation and then close the connection after use. This method will cause frequent establishment and closing of database connections, increasing system overhead. In order to solve this problem, you can use php-fpm connection pool technology to improve database access performance. Principle of connection pool: Connection pool is a caching technology that combines a certain number of databases

PHP-FPM is a commonly used PHP process manager used to provide better PHP performance and stability. However, in a high-load environment, the default configuration of PHP-FPM may not meet the needs, so we need to tune it. This article will introduce the tuning method of PHP-FPM in detail and give some code examples. 1. Increase the number of processes. By default, PHP-FPM only starts a small number of processes to handle requests. In a high-load environment, we can improve the concurrency of PHP-FPM by increasing the number of processes

How to use PHP-FPM to optimize and improve the performance of Phalcon applications. Introduction: Phalcon is a high-performance PHP framework. Combining with PHP-FPM can further improve the performance of applications. This article will introduce how to use PHP-FPM to optimize the performance of Phalcon applications and provide specific code examples. 1. What is PHP-FPMPHP-FPM (PHPFastCGIProcessManager) is a PHP process independent of the web server

Solution for Ubuntu without php-fpm: 1. Add the source address of PHP by executing the "sudo apt-get" command; 2. Check whether there is a php7 package; 3. Install PHP by executing the "sudo apt-get install" command; 4. , modify the configuration to listen on port 9000 to handle nginx requests; 5. Start "php7.2-fpm" through "sudo service php7.2-fpm start".

What is php-fpm? The following article will take you to understand php-fpm and introduce what we need to optimize when optimizing php-fpm. I hope it will be helpful to everyone!
