In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze the principle of Swoole engine, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Over the past six months, I have completed a game server project using both PHP and Java technology stacks. Due to the high frequency of network requests in the project, the PHP technology stack tries to use Swoole engine (event-based high-performance asynchronous parallel network communication engine) to complete part of the game business.
Installation of Swoole
It is easy to install swoole. As it is a Chinese project, many issue can be found on the official website. There are two types of installation:
Compile and install. Go directly to github or gitee to download the official distribution, compile and install, and write the so extension to the php.ini file.
Container installation. The swoole engine is widely used, so there are many containers available on hub, so just select the pull you need.
Specific operation of Baidu can be, a lot of relevant content on the Internet.
Advantages of Swoole engine
Resident in memory. Traditional PHP framework or single file, before processing each request, must do the operation of loading the framework file and configuration. After the request is completed, all resources and memory will be released, so there is no need to worry about memory leakage. However, if the number of requests increases and the concurrency is high, creating resources quickly and releasing them immediately will lead to a sharp decline in the efficiency of PHP programs. Using Swoole does not have this problem: PHP code has a longer life cycle after it is loaded into memory, so that established database connections and other large objects are not released. Only a small amount of code needs to be processed per request, and the code is compiled by the PHP parser and resides in memory only the first time it is run. In the future, it will be loaded directly into OPCODE and let the Zend engine run directly. In addition, PHP can not achieve before, such as database connection pooling, cache connection pooling can be implemented under the Swoole engine. The running efficiency of the system will be greatly improved.
Rapid development. Swoole engine provides PHP language asynchronous multithreaded server, asynchronous TCP/UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file read and write, asynchronous DNS query. Swoole has built-in Http/WebSocket server / client, Http2.0 server.
Cooperative programming mode. Swoole4 can implement asynchronous programs using fully synchronous code. The PHP code does not need to add any additional keywords, and the underlying layer automatically schedules the cooperative program to achieve asynchronous IO.
Process Analysis of Swoole engine
The flowchart of the 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-thread mode and process mode. This article only discusses the process mode. The specific difference between the two is described in the official documentation.
Master process
Used to handle Swoole core events, such as connections from clients, pipes for local communications. There are multiple threads in the master process, each running an instance of the epol function. (since the Worker process is not fork by the Master process, the Worker process may still exist after forcing the kill Master process.)
Reactor thread
The main process of Swoole is a multithreaded program. There is a very important set of threads called Reactor threads. It is the thread that actually handles TCP connections and sends and receives data. After Accept a new connection, the main thread of Swoole assigns the connection to a fixed receiver thread, which is responsible for listening to the socket. When the socket is readable, the data is read, the protocol is parsed, and the request is delivered to the Worker process. Send data to the TCP client when socket is writable
Manager process
The worker/task process in Swoole is Fork and managed by the Manager process. When the child process ends running, the manager process is responsible for recycling the child process to avoid becoming a zombie process. And create a new child process when the server shuts down, the manager process will send a signal to all child processes, informing the child process to shut down the service server reload, the manager process will shut down / restart the child process one by one
Worker process
Swoole provides a perfect process management mechanism, when a Worker process exits abnormally, such as a fatal error in PHP, being mistakenly killed by other programs, or exiting normally after reaching the number of max_request. The main process restarts the new Worker process. Code can be written in a Worker process like normal apache+php or php-fpm. There is no need to write asynchronous callback code like Node.js
Callback function of each process
Callback function in Master:
OnStart
OnShutdown
Callback function in Worker process
OnWorkerStart
OnWorkerStop
OnConnect
OnClose
OnReceive
OnFinish
Callback function in TaskWorker process
OnTask
OnWorkerStart
Callback function in Manager process
OnManagerStart
OnManagerStop
The relationship among Reactor, Worker and TaskWorker
It can be understood that Reactor means nginx,Worker and php-fpm. Reactor threads process network requests asynchronously and in parallel, and then forward them to the Worker process for processing. Reactor and Worker communicate through UnixSocket.
In the application of php-fpm, a task is often delivered asynchronously to queues 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, which integrates task delivery, queue and php task processing process management. The processing of asynchronous tasks can be easily realized through the API provided by the underlying layer. In addition, TaskWorker can also return a result to Worker after the completion of the task.
Swoole's Reactor, Worker and TaskWorker can be closely integrated to provide a more advanced way of use. A more popular metaphor: suppose the Swoole application server is a factory, then Reactor is sales and accepts customer orders. And Worker is the worker. When the sales receive an order, Worker goes to work to produce what the customer wants. TaskWorker can be understood as an administrative staff, who can help Worker do some chores and let Worker concentrate on his work.
The underlying layer assigns a unique ID to the Worker process and the TaskWorker process. Different Worker and TaskWorker processes can communicate through the sendMessage interface.
The division of labor among the process threads in the actual project:
Manager process: responsible for managing worker processes, creating or recycling
Worker process: game logic processing
TaskWorker process: send network packets to clients and close long-inactive tcp connections
Swoole version compatibility
The swoole engine version 1.9.6 was used in the development phase of the project, and later, as the test environment was installed to version 4.3.2, the business code tried to make adjustments. However, the downward compatibility of swoole is very admirable that there is only one code incompatibility problem found in the process: it is a configuration parameter of swoole_server, which is configured with devil numbers in the original version, but to the new version, this number is not defined by macros. Later, I found the macro definition group by looking at the swoole source code, and then modified this configuration. (however, the smooth upgrade of the version is also based on less swoole business code, so it is for reference only.)
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.