OpenSwoole\Process::useQueue ( int $key [, int $mode ] ) : bool

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole
rocess::useQueue ( int $key [, int $mode ] ) : bool

Parameters

msgkey

the ID of the message queue, the default value is ftok(FILE, 1)

mode

2 : competitive mode, all the processes created will compete for the message received by the message queue. Set the mode to $mode | swoole_process::IPC_NOWAIT to make the message queue non-blocking. In the non-blocking mode, it will return immediately when the method push is called in the condition of full queue and the method pop is called in the condition of empty queue.

Return

length

The length of data sent

Description

Create a message queue as the communication method between the parent process and child processes.

You can't use the pipe and message queue the same time.

Before the start of the Swoole Process, you can use the method push and pop to prepare data for the process.

Example

<?php
$child_num = 3;
$child_processes = [];
for($i = 1; $i <= $child_num; $i++)
{
    $process = new OpenSwoole\Process(function($worker){
        echo "the pid of child process is " . $worker->pid . "\n";

        $worker->name("php child process");

        $recv = $worker->pop();

        echo "To child process " . $worker->pid .  " : " . $recv;

        echo "the child process " . $worker->pid . " exited\n";

        exit(0);

    }, FALSE);

    $res = $process->useQueue(0, 2);

    $pid = $process->start();

    $child_processes[(string)$pid] = $process;
}

foreach($child_processes as $pid => $child_process) {
    $child_process->push("From main process : Hello child process {$pid}\n");
}
Last updated on September 1, 2022