Swoole\Process\Pool::__construct

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\\Process::__construct (int $worker_num, int $ipc_type = 0, int $msgqueue_key = 0, bool $enable_coroutine = false)

Parameters

worker_num

How many worker process will be created in the process pool

ipc_type

SWOOLE_IPC_NONE: disable IPC. SWOOLE_IPC_MSGQUEUE: use system msg queue for IPC, for adding new task message into the worker pool. SWOOLE_IPC_SOCKET: use Socket for IPC.

msgqueue_key

The message queue key when using system msg queue.

enable_coroutine

If to enable the coroutine support in the process

Return

Description

Create a pool of processes. There are 3 modes to communicate with and sending new task messages to the worker pool: with MQ like Redis or with system msg queue, or with Socket.

Example

<?php
$pool = new Swoole\Process\Pool(2, SWOOLE_IPC_SOCKET);

$pool->on("Message", function ($pool, $message) {
    echo "Message: {$message}\n";
    $pool->write("hello ");
    $pool->write("world ");
    $pool->write("\n");
});

$pool->listen('127.0.0.1', 8089);
$pool->start();
Last updated on August 31, 2022