当前位置:网站首页>Common interview questions about swoole
Common interview questions about swoole
2022-07-05 15:04:00 【Back end regular developers】
List of articles
- One 、 Why do you use swoole, What pain points in your project can be solved ?
- Two 、 How did you get through swoole Performance enhancing , How to do it? ?
- 3、 ... and 、swoole What is the collaboration process in , How to use it? ? Why can synergy improve concurrency ?
- Four 、 It was used swoole in the future , Will there be a memory leak ? If something happens, how to solve it ?
- 5、 ... and 、swoole and php-fpm The difference between
One 、 Why do you use swoole, What pain points in your project can be solved ?
swoole Is a network communication and asynchronous IO The engine of , A basic library ;
swoole Compared with apache/fpm, Major savings PHP The performance overhead of each creation and destruction of framework and global objects , It is a process resident memory type .
Two 、 How did you get through swoole Performance enhancing , How to do it? ?
Process resident memory :
swoole Itself is a process resident memory , When the process starts PHP The framework and other codes are read and compiled , You don't need to perform the compilation step every time you start , Greatly reduce the running time of the script ;
Connection pool
php-fpm The pattern of php Because all resources are destroyed at the end of each request , Therefore, connection pooling cannot be used ; And based on swoole Process resident memory mode , You can speed up the program through connection pooling ,
Using connection pooling can reduce the response time of programs , It can also effectively protect back-end resources .
Asynchrony can be handled using a coroutine IO
When you need to request multiple data in development , It takes a long time for each piece of data to be requested separately , The conventional php-fpm It's blocking operation , This type of data processing cannot be accelerated ; And based on swoole The program , This kind of business can be parallelized , Request the data source of the back end in parallel , It can greatly optimize the running time of this kind of business .
3、 ... and 、swoole What is the collaboration process in , How to use it? ? Why can synergy improve concurrency ?
Cooperative processes are switched through cooperation rather than preemption , It creates and switches resources such as memory, which is much smaller than threads ( It can be understood as smaller threads );
The use of synergy is through Swoole\Coroutine perhaps Co\ Namespace short names simplify class names to create ;
A coroutine can process tasks asynchronously , Support concurrency , And the resource consumption is small .
Four 、 It was used swoole in the future , Will there be a memory leak ? If something happens, how to solve it ?
swoole Because it is resident memory , Once the resource is loaded, enter , Will always exist in memory . For local variables ,swoole It will be released automatically after the callback function ends ; For global variables (lobal Declared variables ,
static Declared object properties or static variables and super global variables in functions ),swoole It doesn't automatically release ; Therefore, bad operation will lead to memory leakage .
solve :
stay onClose Clean up variables in callback ;
swoole Provides max_request and max_task_request Mechanism : After the process completes the specified number of tasks , Will automatically exit , Achieve the purpose of releasing resources and memory ; Then manager The process will start again worker/task Process to continue processing tasks .
Usage restriction : max_request Can only be used for synchronous blocking 、 Stateless request response server program ; Pure asynchronous Server Should not be set max_request Use Base Mode time max_request It's invalid
5、 ... and 、swoole and php-fpm The difference between
PHP-FPM:
Early versions of PHP There's no built-in WEB The server , It provides SAPI(Server API) Connect with the third party . It's very popular now php-fpm It is through FastCGI Agreement to deal with PHP With a third party WEB Communication between servers . such as Nginx + php-fpm The combination of , It works this way fpm yes Master/Worker Pattern , Start a Master Process listening from Nginx Request , Again fork Multiple Worker The process processes the request . Every Worker The process can only handle one request , The life cycle of a single process is roughly as follows :
Initialization module .
Initialization request . The request here is a request PHP The meaning of executing code , Is not HTTP Request .
perform PHP Script .
End request .
Close the module .
The multi process model depends on the number of processes to solve the concurrency problem , A process can only handle one connection , When starting a large number of processes , Process scheduling consumption may account for CPU Tens of percent or even 100%, such as C10K problem , The multi process model is not enough .
Swoole:
Swoole The same is true of Master/Worker Pattern , The difference is Master There are multiple processes Reactor Threads ,Master It's just an event generator , Responsible for monitoring Socket Event change of handle .Worker Run as a multiprocess , Receive from Reactor Thread request , And execute the callback function (PHP Compiling ). start-up Master The process flow is roughly :
Initialization module .
Initialization request . because swoole Need to pass through cli How to run , So when initializing the request , Will not initialize PHP Global variable of , Such as $_SERVER, $_POST, $_GET etc. .
perform PHP Script . Including the lexical 、 Syntax analysis , Variable 、 function 、 Class initialization, etc ,Master Enter monitor state , It doesn't end the process .
Swoole The principle of acceleration
from Reactor(epoll Of IO Reuse mode ) Responsible for monitoring Socket Event change of handle , Solve the problem of high concurrency .
Save... By resident memory PHP Code initialization time , When using heavy frames , use swoole The acceleration effect is very obvious .
The contrast is different
PHP-FPM
Master The main process / Worker Multi process mode .
start-up Master, adopt FastCGI Protocol listening from Nginx Requests for transmission .
Every Worker The process corresponds to only one connection , Used to perform complete PHP Code .
PHP Code execution completed , The occupied memory will be destroyed , The next request requires re initialization and other cumbersome operations .
Used only for HTTP Server.
Swoole
Master The main process ( By multiple Reactor Thread composition )/ Worker Multi process ( Or multithreading ) Pattern
start-up Master, initialization PHP Code , from Reactor monitor Socket Event change of handle .
Reactor The main thread is responsible for the balance of sub threads ,Manager Process management Worker Multi process , Include TaskWorker The process of .
Every Worker from Reactor Request , You only need to execute the part of the callback function PHP Code .
Only in Master Execute once at startup PHP Initialization code ,Master Enter monitor state , It doesn't end the process .
Not only for HTTP Server, You can also create TCP Connect 、WebSocket Connect .
边栏推荐
- leetcode:881. lifeboat
- No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
- Coding devsecops helps financial enterprises run out of digital acceleration
- 面试突击62:group by 有哪些注意事项?
- 漫画:优秀的程序员具备哪些属性?
- How to open an account of qiniu securities? Is it safe to open an account?
- 超级哇塞的快排,你值得学会!
- 【NVMe2.0b 14-9】NVMe SR-IOV
- Stm32+bh1750 photosensitive sensor obtains light intensity
- 【NVMe2.0b 14-9】NVMe SR-IOV
猜你喜欢
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
Ctfshow web entry explosion
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
[detailed explanation of Huawei machine test] character statistics and rearrangement
随机推荐
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
机器学习笔记 - 灰狼优化
[recruitment position] infrastructure software developer
PHP high concurrency and large traffic solution (PHP interview theory question)
30岁汇源,要换新主人了
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Cartoon: programmers don't repair computers!
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
P1451 求细胞数量/1329:【例8.2】细胞
面试突击62:group by 有哪些注意事项?
超级哇塞的快排,你值得学会!
Select sort and bubble sort
Isn't it right to put money into the external market? How can we ensure safety?
百亿按摩仪蓝海,难出巨头
R 熵权法计算权重及综合得分
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
Fr exercise topic - simple question
js亮瞎你眼的日期选择器