search
HomePHP FrameworkThinkPHPThink-Swoole tutorial configuration, work process, Ctrip charm and understanding Swoole process mode

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
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools