Php-fpm process model
Let’s first look at how phP-fpm works:
① : Initiate an HTTP request.
2: Nginx forward the user’s request to phP-fpm:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
Copy the code
In this case, the request ending in.php is forwarded to the local port 9000, which is the default listening port for PHP-Fpm.
③ : PhP-FPM
The Master process starts the Worker Pool, and the Worker Pool maintains multiple Worker processes. The Worker process listens for requests and establishes connections, and implements the FastCGI protocol to communicate and obtain data. PHP scripts are executed through the embedded PHP interpreter.
'Here, each Worker process processes user requests in a blocked way, and the same Worker can only process one request at a time. Due to limited hardware resources, it is difficult to support a large number of concurrent requests. The common "502 Bad Gateway" is caused by no available process of PHP-fpmCopy the code
④ : return the result of PHP execution to Nginx.
⑤ : Nginx returns the data to the browser.
Swoole process model
In Swoole, we look at process models to understand how it works. There are two types of process models.
-
- Swoole Process model in SWOOLE_PROCESS mode
(1) Master process: we start the process, is a multi-threaded process, including the internal MainReactor thread, Reactor thread, and other threads, at the same time responsible for creating the Manager process to assist the management of other processes, the Master process is mainly responsible for network communication related functions. (About the choice of network communication I/O model, there are five I/O models in Unix network programming, and Swoole uses the Reactor model, which is based on IO multiplexing model.)
② : MainReactor thread: Accepts the connection from the client and assigns the connection to the low-load Reactor thread.
③ : Reactor thread: maintains the CLIENT TCP connection, processes network IO, processes protocols, sends and receives data, buffers, splices, and separates the data sent by the TCP client into a complete request packet, and does not execute any PHP code. (The Reactor thread listens for data sent by the Worker process to the main process, and then sends the data to the client.) .
④ : Manager process: is responsible for creating/recycling worker/task process.
⑤ : The Worker process is responsible for processing the data packets sent by the Reactor thread through the Unix Socket. (Here we write our business logic and send the data to the main process after the execution is complete.)
⑥ : Task process: responsible for processing the tasks delivered by the Worker process, and send data to the Worker process through the onFinish event after completion.
The key points involved are:
Swoole Inter-process Communication (IPC) between internal processes is implemented using Unix sockets. Swoole NETWORK communication I/O model uses Reactor model.
Request processing process:
The client initiates a request
The connection is set up by the MainReactor thread. Then the request is assigned to the Reactor thread. The Reactor thread starts to listen to the request data sent by the client.
❸ : After receiving a complete request data, the Reactor thread sends the data to the Worker process for processing (the data header records the Reactor information). The Worker process executes our business logic code, and then sends the results to the Master process through the Unxi Socket. The data header contains Reactor information and requested FD information.
Positivity: Each Reactor thread in the main process is responsible for monitoring the data packets sent by the Worker process, but the data sent by each Worker process is monitored by only one Reactor thread, and the data is returned to the client by the Reactor. (If you want to change the connection status, call epoll_ctl when you find the corresponding Reactor thread listening to the connection.)
Swoole Process model in SWOOLE_BASE mode
① : Manager process: In SWOOLE_BASE mode, Worker processes are managed by the Manager process. In this mode, if worker_num is set to 1 and Task and MaxRequest are not used, The underlying layer will create a separate Worker process directly, without creating a Manager process.
② : Worker process: Each Worker process is responsible for both the Reactor thread and Worker process in SWOOLE_PROCESS mode.
③ : Task process: responsible for processing the tasks delivered by the Worker process, and send the data to the Worker process through the onFinish event after the completion of execution.
Swoole’s two process running models are described above. Let’s sort out the features:
A shift in development thinking
We analyze phP-Fpm and Swoole modes. In phP-Fpm mode, most of the code is executed in a separate process. We don’t need to care about resource recycling, how processes communicate with each other, network communication I/O model, how the server and client establish and maintain connection, etc. When we moved into Swoole development, we had to step out of our comfort zone and learn more about the operating system, such as:
- What is Reactor model?
- How Processes communicate with each other (IPC)
- Refer to the Swoole process model above for request processing changes.
- .
Swoole is easy to use. It’s easy to start a service by following the official document demo and then write the logic to the document where it tells you where to go. But to better adapt to Swoole’s development model, you need to understand its process model. Swoole will make you more comfortable with it.