共被编辑 2 次

版本 更新时间 贡献者 编辑原因 操作
#r2 4月26日 MrWhite1 更新问题 查看

为什么我的swoole是master进程直接拉起worker进程而不经过manager进程?

题目描述

通过上网查阅资料我得到这么一个理念:swoole server启动时,由master进程拉起manager进程,manager进程拉起worker进程,我自己试验后得到的结果却并非如此

相关代码

<?php
$server = new swoole_server("127.0.0.1",8088,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
$server->set(array(

'worker_num' => 2

));

$server->on('connect', function ($serv, $fd){ });

$server->on('receive', function ($serv, $fd, $from_id, $data){ });

$server->on('close', function ($serv, $fd){ });

// 在交互进程中放入一个数据。
$server->BaseProcess = "I'm base process.";

// 为了便于阅读,以下回调方法按照被起调的顺序组织
// 1. 首先启动Master进程
$server->on("start", function (\swoole_server $server){

echo "On master start.".PHP_EOL;
// 先打印在交互进程写入的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;
// 修改交互进程中写入的数据
$server->BaseProcess = "I'm changed by master.";
// 在Master进程中写入一些数据,以传递给Manager进程。
$server->MasterToManager = "Hello manager, I'm master.";

});

// 2. Master进程拉起Manager进程
$server->on('ManagerStart', function (\swoole_server $server){

echo "On manager start.".PHP_EOL;
// 打印,然后修改交互进程中写入的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;
$server->BaseProcess = "I'm changed by manager.";
// 打印,然后修改在Master进程中写入的数据
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
$server->MasterToManager = "This value has changed in manager.";

// 写入传递给Worker进程的数据
$server->ManagerToWorker = "Hello worker, I'm manager.";

});

// 3. Manager进程拉起Worker进程
$server->on('WorkerStart', function (\swoole_server $server, $worker_id){

echo "Worker start".PHP_EOL;
// 打印在交互进程写入,然后在Master进程,又在Manager进程被修改的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

// 打印,并修改Master写入给Manager的数据
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
$server->MasterToManager = "This value has changed in worker.";

// 打印,并修改Manager传递给Worker进程的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
$server->ManagerToWorker = "This value is changed in worker.";

});

// 4. 正常结束Server的时候,首先结束Worker进程
$server->on('WorkerStop', function(\swoole_server $server, $worker_id){

echo "Worker stop".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

// 5. 紧接着结束Manager进程
$server->on('ManagerStop', function (\swoole_server $server){

echo "Manager stop.".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

// 6. 最后回收Master进程
$server->on('shutdown', function (\swoole_server $server){

echo "Master shutdown.".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

$server -> start();

我得到了如下结果,master进程启动后直接拉起了worker进程,这是为什么呢?希望热心大神告知,感谢!

clipboard.png

#r1 4月26日 MrWhite1 创建问题 查看

为什么我的swoole是master进程直接拉起worker进程而不经过manager进程?

题目描述

通过上网查阅资料我得到这么一个理念:swoole server启动时,由master进程拉起manager进程,manager进程拉起worker进程,我自己试验后得到的结果却并非如此

题目来源及自己的思路

代码来源github

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
<?php
$server = new swoole_server("127.0.0.1",8088,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
$server->set(array(

'worker_num' => 2

));

$server->on('connect', function ($serv, $fd){ });

$server->on('receive', function ($serv, $fd, $from_id, $data){ });

$server->on('close', function ($serv, $fd){ });

// 在交互进程中放入一个数据。
$server->BaseProcess = "I'm base process.";

// 为了便于阅读,以下回调方法按照被起调的顺序组织
// 1. 首先启动Master进程
$server->on("start", function (\swoole_server $server){

echo "On master start.".PHP_EOL;
// 先打印在交互进程写入的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;
// 修改交互进程中写入的数据
$server->BaseProcess = "I'm changed by master.";
// 在Master进程中写入一些数据,以传递给Manager进程。
$server->MasterToManager = "Hello manager, I'm master.";

});

// 2. Master进程拉起Manager进程
$server->on('ManagerStart', function (\swoole_server $server){

echo "On manager start.".PHP_EOL;
// 打印,然后修改交互进程中写入的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;
$server->BaseProcess = "I'm changed by manager.";
// 打印,然后修改在Master进程中写入的数据
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
$server->MasterToManager = "This value has changed in manager.";

// 写入传递给Worker进程的数据
$server->ManagerToWorker = "Hello worker, I'm manager.";

});

// 3. Manager进程拉起Worker进程
$server->on('WorkerStart', function (\swoole_server $server, $worker_id){

echo "Worker start".PHP_EOL;
// 打印在交互进程写入,然后在Master进程,又在Manager进程被修改的数据
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

// 打印,并修改Master写入给Manager的数据
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
$server->MasterToManager = "This value has changed in worker.";

// 打印,并修改Manager传递给Worker进程的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
$server->ManagerToWorker = "This value is changed in worker.";

});

// 4. 正常结束Server的时候,首先结束Worker进程
$server->on('WorkerStop', function(\swoole_server $server, $worker_id){

echo "Worker stop".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

// 5. 紧接着结束Manager进程
$server->on('ManagerStop', function (\swoole_server $server){

echo "Manager stop.".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

// 6. 最后回收Master进程
$server->on('shutdown', function (\swoole_server $server){

echo "Master shutdown.".PHP_EOL;
// 分别打印之前的数据
echo "server->ManagerToWorker = ".$server->ManagerToWorker.PHP_EOL;
echo "server->MasterToManager = ".$server->MasterToManager.PHP_EOL;
echo "server->BaseProcess = ".$server->BaseProcess.PHP_EOL;

});

$server -> start();

你期待的结果是什么?实际看到的错误信息又是什么?

我得到了如下结果,master进程启动后直接拉起了worker进程,这是为什么呢?希望热心大神告知,感谢!

clipboard.png