Swoole v4.7 版本新特性预览之 Process\Pool::detach()

简介: Process\Pool 是 Swoole 提供的进程池,基于 Server 的 Manager 管理进程模块实现,可管理多个工作进程。

Process\Pool 是 Swoole 提供的进程池,基于 Server 的 Manager 管理进程模块实现,可管理多个工作进程。

该模块的核心功能为进程管理,相比 Process 实现多进程,Process\Pool 更加简单,封装层次更高,开发者无需编写过多代码即可实现进程管理功能,配合 Coroutine\Server 可以创建纯协程风格的,能利用多核 CPU 的服务端程序。

在 4.7 版本中,对 Process\Pool 增加了一个 detach 方法,这个方法名看起来很眼熟吧?

Http\Response 中也有一个 detach 方法,它的作用是分离响应对象。使用此方法后,$response 对象销毁时不会自动 end,与 Http\Response::createServer->send 配合使用。


方法作用


那么Process\Pool::detach()的作用也就很明显了:

将进程池内当前 Worker 进程脱离管理,底层会立即创建新的进程,老的进程不再处理数据,由应用层代码自行管理生命周期。

示例代码


下面来看一下示例代码:

use Swoole\Process;
use Swoole\Coroutine;
$pool = new Process\Pool(2);
$pool->set(['enable_coroutine' => true]);
$pool->on('WorkerStart', function (Process\Pool $pool, $workerId) {
    static $running = true;
    Process::signal(SIGTERM, function () use (&$running) {
        $running = false;
        echo "TERM\n";
    });
    echo("[Worker #{$workerId}] WorkerStart, pid: " . posix_getpid() . "\n");
    $i = 0;
    while ($running) {
        Coroutine::sleep(1);
        $i++;
        if ($i == 5) {
            $pool->detach();
        } elseif ($i == 10) {
            break;
        }
    }
});
$pool->on('WorkerStop', function (Process\Pool $pool, $workerId) {
    echo("[Worker #{$workerId}] WorkerStop, pid: " . posix_getpid() . "\n");
});
$pool->start();

WorkerStart 中通过 Process::signal 设置一个异步信号监听,可以通过发送 SIGTERM 信号来停止该服务。

服务运行中,当$i等于 5 时,让当前进程脱离管理;同时底层会创建新的进程来维持worker_num数量;当$i等于 10 时,结束该进程。

所以会得到以下输出:

[Worker #0] WorkerStart, pid: 75050
[Worker #1] WorkerStart, pid: 75051
[Worker #0] WorkerStart, pid: 75054
[Worker #1] WorkerStart, pid: 75055
[Worker #0] WorkerStop, pid: 75050
[Worker #1] WorkerStop, pid: 75051
[Worker #1] WorkerStart, pid: 75056
[Worker #0] WorkerStart, pid: 75057

在以上代码中相当于维护了 4 个进程,在一次退出后又会重新拉起两个新的进程,如是往复。

在使用时就需要特别注意逻辑问题,否则可能会导致无限创建新的进程。

目录
相关文章
|
开发工具
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(一)
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(一)
830 0
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(一)
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
287 0
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
|
28天前
|
存储 缓存 安全
go sync.Pool 设计与实现
go sync.Pool 设计与实现
23 2
|
10月前
|
编译器 Go 开发工具
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
174 0
|
12月前
|
Swift
Swift Debug 和 Release 中 print() 函数调试切换
Swift Debug 和 Release 中 print() 函数调试切换
63 0
|
Shell
奥比中光ROS启动节点运行异常退出:[camera/driver-2] process has finished cleanly
奥比中光ROS启动节点运行异常退出:[camera/driver-2] process has finished cleanly
442 0
奥比中光ROS启动节点运行异常退出:[camera/driver-2] process has finished cleanly
|
IDE 开发工具
Waiting for another flutter command to release the startup lock... 异常解决
平时我们在开发flutter过程中,在执行`flutter packages get`命令之后,如果运气不好的,命令没有执行成功的话,我们就会遇到这个错误提示: ``` Waiting for another flutter command to release the startup lock... ```
首次运行Flutter失败报错(Finished with error: Gradle task assembleDebug failed with exit code 1)
首次运行Flutter失败报错(Finished with error: Gradle task assembleDebug failed with exit code 1)
408 0
首次运行Flutter失败报错(Finished with error: Gradle task assembleDebug failed with exit code 1)
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
392 0
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
|
开发工具
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(二)
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(二)
318 0
【错误记录】Flutter 插件报错 ( Methods marked with @UiThread must be executed on the main thread. | 更新最新 SDK )(二)