Analysis and learning of PHP and swoole

Time:2022-6-15

php

PHP we use is mainly used for web development. We call PHP FPM to process the business logic of the server through nginx, Apache and other server-side programs. After processing, PHP withdraws memory and returns the results. It can be seen that an HTTP needs to load all PHP files once. This is also one of the shortcomings of PHP FPM at present, and PHP FPM is not suitable for service-side development.

The following is the PHP FPM running process I casually found on the Internet. Please refer to it for understanding

http://www.test.cc
        |
      Nginx
        |
Route to http://www.test.cc/index.php
        |
Load the fast CGI module of nginx
        |
Fast CGI listens for 127.0.0.1:9000 addresses
        |
www.test. com/index. PHP request arrives at 127.0.0.1:9000
        |
PHP FPM listening 127.0.0.1:9000
        |
PHP FPM receives the request and enables the worker process to process the request
        |
PHP FPM finishes processing the request, revokes the memory, and returns it to nginx
        |
Nginx returns the results to the browser via http

To summarize, I understand the advantages and disadvantages of PHP FPM

advantage:

  1. Simple deployment
  2. Convenient debugging
  3. There are many projects based on traditional PHP, which are easy to refer to

Disadvantages:

  1. Load all project files for each HTTP request
  2. PHP FPM has poor performance and concurrency
  3. Core asynchronous network not supported

swoole

The official introduction of swoole

Swoole is a PHPSynergetic processHigh performance network communication engine, usingC/C++Language, providing network server and client modules of various communication protocols
, enabling PHP developers to write high-performance asynchronous concurrent TCP, UDP, UNIX socket, HTTP, and websocket services. Swoole can be widely used in Internet, mobile communication, enterprise software, cloud computing, online games, Internet of things (IOT), Internet of vehicles, smart home and other fields. Using PHP + spool as the network communication framework can greatly improve the efficiency of enterprise IT R & D teams^ [1]^

Advantages of swoole over PHP FPM:

  • Can set up tcp/udp/unixsocket server
  • Native support for millisecond timers
  • Asynchronous / synchronous tcp/udp network client, asynchronous mysql, asynchronous redis
  • High performance co process improves the effect and performance of asynchronous IO
  • Resident cli running mode, without loading the project code every time

Compared with PHP FPM, swoole has the following disadvantages:

  • Xdebug and manual dump are not supported
  • Few related documents
  • It is difficult to get started. Most phpers do not understand tcp/ip network protocol, multi process / multi thread, asynchronous IO, etc

The detailed swoole introduction can be viewed in the easyswool documentEasyswool documentation

How to learn swoole

  1. Take a brief look at the official documentation of swoole
  2. Take a look at and implement the two swoole tutorials of singwa
  3. Official entry video for esaysooleEasyswoole getting started video
  4. Take a look at the open source swoole project and study how others use swoole to implement business logichttps://www.fashop.cn/ | Blog: woann chat chat chat system based on laravels and layim
  5. At this point, you should be able to use swoole PHPer

My actual project

Deep optimization process of laravel project
Jin chat chat chat system based on easysoole and layim

This work adoptsCC agreement, reprint must indicate the author and the link to this article

No reprinting without permission — coolie Kobayashi,

Recommended Today

[statistical learning methods | notes] Chapter 1 Statistical Learning Method Theory

catalogue 1 Statistical Learning 2 supervised learning 2.1 basic concepts 2.2 formalization of problems 3. Three elements of statistical learning 3.1 model 3.2 strategy 3.3 algorithm 4 model evaluation and model selection 4.1 training error and test error 4.2 over fitting and model selection Regularization and cross validation — two commonly used model selection methods […]