Swoole 启动一个服务,开启了哪些进程和线程?
serv.php (推荐学习: swoole视频教程)
<?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();
上面的代码简单说下,创建了一个 TCP 服务器,启动了 3 个 worker 进程, 3 个 task 进程,因为启用了 task 功能,所以必须注册 onTask、onFinish 2 个事件的回调函数。
咱们运行一下:
使用 ps 查看下: