Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between fpm and swoole of php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "what is the difference between fpm and swoole in php". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "what is the difference between fpm and swoole in php" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

Differences: 1."PHP-FPM" is only applicable to HTTPServer, while swoole is not only used for HTTPServer, but also for establishing TCP connections;2."PHP-FPM" monitors Nginx transmission requests through FastCGI protocol, while swoole monitors event changes through Reactor.

Operating environment of this tutorial: Windows 10 system, PHP7.1 version, DELL G3 computer

What is the difference between php-fpm and swoole?

I.PHP-FPM

Earlier versions of PHP did not have a built-in WEB server, but provided SAPI (Server API) for third parties to interface with. Php-fpm, which is very popular nowadays, handles communication between PHP and third-party WEB servers through FastCGI protocol.

For example, Nginx + php-fpm combination, this way running fpm is Master/Worker mode, start a Master process to listen to requests from Nginx, and then fork multiple Worker processes to process requests. Each Worker process can handle only one request, and the life cycle of a single process is roughly as follows:

1. Initialize the module.

2. Initialize request. The request here is a request for PHP to execute code, not an HTTP request.

3. Execute PHP scripts.

4. End request.

5. Close the module.

Swoole also uses Master/Worker mode, the difference is that the Master process has multiple Reactor threads, and the Master is just an event generator responsible for listening for event changes in Socket handles. Worker runs as a multiprocess, receives requests from Reactor threads, and executes callback functions (written in PHP). The process to start the Master process is roughly as follows:

1. Initialize the module.

2. Initialize request. Because swoole needs to run in cli mode, PHP global variables such as $_SERVER, $_POST, $_GET, etc. are not initialized when the request is initialized.

3. Execute PHP scripts. Including morphology, syntax analysis, variables, functions, class initialization, etc., Master enters the listening state and does not end the process.

Swoole acceleration principle

Reactor (IO multiplexing mode of epoll) is responsible for monitoring the event change of Socket handle to solve the problem of high concurrency.

Saving PHP code initialization time by memory-resident means, using swoole acceleration is very obvious when using bulky frameworks.

II. comparing different

PHP-FPM

Master/ Worker multi-process mode.

Start Master and listen for requests transmitted from Nginx via FastCGI protocol.

Each Worker process has only one connection to execute the complete PHP code.

PHP code execution is complete, the occupied memory will be destroyed, the next request needs to be re-initialized and other cumbersome operations.

Only for HTTP servers.

Swoole

Master (composed of multiple Reactor threads)/ Worker multi-process (or multi-threaded) mode

Start Master, initialize PHP code, and Reactor listens for event changes in Socket handles.

The Reactor main thread is responsible for balancing child multithreads, and the Manager process manages Worker multiprocesses, including TaskWorker processes.

Each Worker accepts requests from Reactor and only needs to execute the PHP code for the callback function part.

Executing PHP initialization code only once when Master starts, Master enters listening state and does not end the process.

Not only can it be used for HTTP Server, but also TCP connection and WebSocket connection can be established.

Read here, this article "php fpm and swoole what is the difference" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report