Home  >  Article  >  PHP Framework  >  Think-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process mode

Think-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process mode

藏色散人
藏色散人forward
2020-09-19 15:21:502840browse

The following tutorial column will introduce you to the configuration, working process, Ctrip charm and understanding of the Swoole process mode of the Think-Swoole tutorial. I hope it will be helpful to friends in need! Think-Swoole configuration, work process, Ctrip charm and understanding Swoole process mode

Configuration file:

app/config/swoole.php

'server'     => [
    'host'      => env('SWOOLE_HOST', '0.0.0.0'), // 监听地址
    'port'      => env('SWOOLE_PORT', 9501), // 监听端口
    'mode'      => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
    'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
    'options'   => [ // 都是给 Swoole 服务的配置,可以根据 Swoole 手册额外增加其它的 Swoole 配置
        'pid_file'              => runtime_path() . 'swoole.pid', //服务启动以后进程 ID 存放文件
        'log_file'              => runtime_path() . 'swoole.log', //Swoole 的日志文件
        'daemonize'             => false, //守护进程模式设置,true 后台运行
        // Normally this value should be 1~4 times larger according to your cpu cores.
        'reactor_num'           => swoole_cpu_num(), //后台启动的 Reactor 线程数
        'worker_num'            => swoole_cpu_num(), //设置启动的 Worker 进程数
        'task_worker_num'       => swoole_cpu_num(), //配置 Task 进程数
        'enable_static_handler' => true, //开启静态文件请求处理功能,需配合 document_root
        'document_root'         => root_path('public'), //配置静态文件根目录
        'package_max_length'    => 20 * 1024 * 1024, //设置最大数据包尺寸,单位为字节
        'buffer_output_size'    => 10 * 1024 * 1024, //配置发送输出缓存区内存尺寸
        'socket_buffer_size'    => 128 * 1024 * 1024, //用于设置客户端连接最大允许占用内存数量
    ],
],

Working process:

'worker_num' => swoole_cpu_num(),

This configuration is to set the working process. swoole_cpu_num() is to get the number of local CPU cores. If it is manually set to 1, then There are two requests that need to be processed at the same time. Only one can be processed at a time, and the other is in a waiting state. After the first one is processed, the second one will be processed immediately, but they still belong to the same process. The process numbers of the two requests are the same. of. How to set it to 2, then 2 requests can be processed at the same time, and there will be two different process numbers.

Coroutine

In the Swoole configuration file, there is another option to configure the coroutine:

'coroutine'  => [
        'enable' => true,
        'flags'  => SWOOLE_HOOK_ALL,
    ],
'enable' => ; true means starting the coroutine. Assume that 3 requests (or more) need to be processed at the same time. Even if the worker process is set to 1, these three requests can be processed at the same time, but their process numbers are the same, because the worker process is still one. This is the charm of Swoole coroutines.

Understand the Swoole process mode

Configure the number of working processes to 1, then start the service through the command php think swoole, open a new command window and execute ps -ef | grep swoole Check the process status, as shown in the figure below:

When Swoole starts, it will first start a master main process, and then start a manager management sub-process. These two processes The requested work will not be processed, and the processing of the request is handed over to the manager's sub-process worker. As can be seen in the above figure, the process number of the master main process is 30665, the parent process of the manager sub-process 30666 is 30665, and the parent processes of the task process and worker process are both 30666.

Think-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process modeConfigure the number of worker processes to 2, restart the Swoole service, and check the process status again:

It can be seen that there are two worker process processes.

Think-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process modeExecute pstree -p 31568, you can get the following relationship diagram:

The above is the detailed content of Think-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:阿dai哥. If there is any infringement, please contact admin@php.cn delete