Swoole从入门到入土(24)——多进程[进程管理器ProcessManager]

Swoole提供的进程管理器ProcessManage,基于 ProcessPool 实现。可以管理多个进程。相比与 ProcessPool,可以非常方便的创建多个执行不同任务的进程,并且可以控制每一个进程是否要处于协程环境。

注意:SwooleProcessManager只有在4.5.3及以上版本可用,如果碰到class not found错误的时候,检查一下自己当下的swoole版本。

示例:

use SwooleProcessManager;
use SwooleProcessPool;

$pm = new Manager();

for ($i = 0; $i < 2; $i++) {
    $pm->add(function (Pool $pool, int $workerId) {
    });
}

$pm->start();

成员函数

1) __construct():构造方法

SwooleProcessManager::__construct(int $ipcType = SWOOLE_IPC_NONE, int $msgQueueKey = 0);

$ipcType:进程间通信的模式,和 ProcessPool 的 $ipc_type 一致【默认为 0 表示不使用任何进程间通信特性】

$msgQueueKey:消息队列的 key,和 ProcessPool 的 $msgqueue_key 一致

2) setIPCType():设置工作进程之间的通信方式。

SwooleProcessManager->setIPCType(int $ipcType): self;

$ipcType:进程间通信的模式

3) getIPCType():获取工作进程之间的通信方式。

SwooleProcessManager->getIPCType(): int;

4) setMsgQueueKey():设置消息队列的 key。

SwooleProcessManager->setMsgQueueKey(int $msgQueueKey): self;

$msgQueueKey:消息队列的 key

5) getMsgQueueKey():获取消息队列的 key。

SwooleProcessManager->getMsgQueueKey(): int;

6) add():增加一个工作进程。

SwooleProcessManager->add(callable $func, bool $enableCoroutine = false): self;

$func:当前进程执行的回调函数

$enableCoroutine:是否为这个进程创建协程来执行回调函数

7) addBatch():批量增加工作进程。

SwooleProcessManager->addBatch(int $workerNum, callable $func, bool $enableCoroutine = false): self

$workerNum:批量增加进程的个数

$func:这些进程执行的回调函数

$endableCoroutine:是否为这些进程创建协程来执行回调函数

8) start():启动工作进程。

SwooleProcessManager->start(): void

---------------------------  我是可爱的分割线  ----------------------------

最后博主借地宣传一下,漳州编程小组招新了,这是一个面向漳州青少年信息学/软件设计的学习小组,有意向的同学点击链接,联系我吧。

原文地址:https://www.cnblogs.com/ddcoder/p/14243354.html