Swoole start a service, which opened the process and thread?

table of Contents

Outline

Swoole start a service, which opened the process and thread?

To solve this problem, we start a simple service, take a look at exactly what started the process and thread?

Then run in conjunction with the flowchart official website, categorize each process and thread.

After the service starts to print out the current version and the current audit Swoole CPU.

Swoole print version, is so that we can download this version to run the code.

Print CPU core number, because this parameter will be used below.

Ado, a direct look at the code.

Code

serv.php

<?php

class Server
{
    private $serv;

    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9502);
        $this->serv->set([
            'worker_num'      => 3,
            'task_worker_num' => 3,
        ]);
        $this->serv->on('Start', function ($serv) {
            echo "SWOOLE:".SWOOLE_VERSION . " 服务已启动".PHP_EOL;
            echo "SWOOLE_CPU_NUM:".swoole_cpu_num().PHP_EOL;
        });
        $this->serv->on('Receive', function ($serv, $fd, $from_id, $data) { });
        $this->serv->on('Task', function ($serv, $task) { });
        $this->serv->on('Finish', function ($serv, $task_id, $data) {});
        $this->serv->start();
    }
}
$server = new Server();

Under the above code simply creates a TCP server, start the three worker processes, three task process, because the task is enabled, so you must register onTask, onFinish callback function two events.

Let's run it:

Use ps to view the next:

16390 16389 is the parent process.

16393,16394,16395,16396,16397,16398 parent process is 16390.

There are not found, 16391,16392 go now? Is not it strange.

Pstree then see the next:

Out of it, 16391,16392 and 16390 process is a thread level.

Now that we know, start this service uses 8 process, two threads.

Together we look at the document's official Swoole Server:

https://wiki.swoole.com/wiki/page/p-server.html

Look at this picture:

Through the above chart, we can conclude that:

16389 is the Master process.

16390 is the Manager process.

16391,16392 is Reactor thread.

16393,16394,16395,16396,16397,16398 including three Worker process, three Task process.

summary

First, why is three Worker process, three Task process?

Because, in the creation of services that we are set worker_num = 3, task_worker_num = 3.

worker_num 如果不进行设置,默认为 SWOOLE_CPU_NUM,在上面咱们打印出来了,默认为 2,最大不超过,SWOOLE_CPU_NUM * 1000,具体详情,看官方文档。

worker_num 文档:

https://wiki.swoole.com/wiki/page/275.html

task_worker_num 文档:

https://wiki.swoole.com/wiki/page/276.html

二、为什么是 2 个 Reactor 线程?它是干什么的?

因为,Reactor 线程数,默认为 SWOOLE_CPU_NUM,也可以通过 reactor_num 参数进行设置。

reactor_num 文档:

https://wiki.swoole.com/wiki/page/281.html

它是真正处理 TCP 连接,收发数据的线程。

Reactor线程 文档:

https://wiki.swoole.com/wiki/page/347.html

三、Reactor、Worker、TaskWorker 的关系是什么样的?

一个通俗的比喻,假设Server就是一个工厂,那Reactor就是销售,接受客户订单。而Worker就是工人,当销售接到订单后,Worker去工作生产出客户要的东西。而TaskWorker可以理解为行政人员,可以帮助Worker干些杂事,让Worker专心工作。

官方已经解释的很详细了,看官方文档吧:

https://wiki.swoole.com/wiki/page/163.html

如果你想学习 Swoole 可以看下这个 《Swoole 文章汇总(10 篇)》

本文欢迎转发,转发请注明作者和出处,谢谢!

Guess you like

Origin www.cnblogs.com/xinliangcoder/p/11075473.html