Home > Article > PHP Framework > Quick introduction to sharing the principles of Swoole engine
In the past six months, I have completed a game server project using PHP and Java technology stacks. Since there are high-frequency network requests in the project, the PHP technology stack tried to use the Swoole engine (a high-performance event-based asynchronous parallel network communication engine) to complete part of the game business.
Recommended (free): swoole
Swoole installation
Installing swoole is very simple, Since it is a project done by Chinese people, answers to many issues can be found in the official website documents. There are two types of installation:
Just click on Baidu for specific instructions. There is a lot of related content on the Internet.
Advantages of Swoole engine
Swoole engine process analysis
The flow chart of Swoole operation is as follows:
Threads or processes in Swoole
The structure diagram is as follows:
##The Swoole engine is divided into two modes: single-threaded mode and process mode. This article only discusses process mode. The specific differences between the two are explained in the official documentation.Master process is used to handle swoole core events, such as connections from clients and local communication pipes. There are multiple threads in the master process, and each thread runs an instance of the epol function. (Since the Worker process is not forked by the Master process, the Worker process may still exist after forcibly killing the Master process) Reactor threadSwoole's main process is a multi-thread program of. There is a very important group of threads called Reactor threads. It is the thread that actually handles TCP connections and sends and receives data.
After accepting a new connection, Swoole's main thread will assign the connection to a fixed Reactor thread, and this thread will be responsible for monitoring the socket. Read the data when the socket is readable, perform protocol analysis, and deliver the request to the Worker process. Send data to the TCP client when the socket is writable
When the child process ends, the manager process is responsible for recycling the child process to avoid becoming a zombie process. And create a new sub-process
When the server is shut down, the manager process will send signals to all sub-processes to notify the sub-processes to shut down the service
When the server reloads, the manager process will shut down/restart the sub-processes one by one
Callback function of each process
Callback function in Master:Callback function in the Manager process
The relationship between Reactor, Worker and TaskWorker
It can be understood that Reactor is nginx and Worker is php-fpm. The Reactor thread processes network requests asynchronously and in parallel, and then forwards them to the Worker process for processing. Reactor and Worker communicate through UnixSocket.
In php-fpm applications, a task is often delivered asynchronously to a queue such as Redis, and some PHP processes are started in the background to process these tasks asynchronously. TaskWorker provided by Swoole is a more complete solution that integrates task delivery, queue, and PHP task processing process management. Asynchronous task processing can be implemented very simply through the API provided by the underlying layer. In addition, TaskWorker can also return a result to the Worker after the task execution is completed.
Swoole's Reactor, Worker, and TaskWorker can be closely combined to provide more advanced usage methods. A more popular metaphor: Assume that the Swoole application server is a factory, and the Reactor is sales, accepting customer orders. The Worker is the worker. When the salesperson receives the order, the Worker goes to work to produce what the customer wants. The TaskWorker can be understood as an administrative staff, which can help the Worker do some chores so that the Worker can concentrate on work.
The bottom layer will assign a unique ID to the Worker process and TaskWorker process. Different Worker and TaskWorker processes can communicate through the sendMessage interface.
The division of labor of each process thread in the actual project:
Swoole version compatibility
The swoole engine version used in the development stage of this project was 1.9.6. Later, because the test environment was installed to version 4.3.2, we tried to adjust the business code. However, the backward compatibility of swoole is very admirable. In this process, only one code incompatibility problem was discovered: a configuration parameter related to swoole_server. In the original version, it was configured using devil numbers, but in the new version Version, this number was not macro-defined. Later, I found the macro definition group by checking the swoole source code, and then modified this configuration. (However, the smooth version upgrade is also based on swoole's relatively small business code, so it is for reference only
More related learning recommendations:swoole tutorial
The above is the detailed content of Quick introduction to sharing the principles of Swoole engine. For more information, please follow other related articles on the PHP Chinese website!