Swoole\Process::push ( string $data ) : int

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Process::push ( string $data ) : int

Parameters

data

The data to push to the queue, the default value is 8192 Bytes and the max value is 65536 Bytes.

Return

success

If failed, it returns FALSE. Or it returns TRUE. In the blocking mode, if the queue is full, the call of this method will block. In the non-blocking mode, if the queue is full, the call of this method will return FALSE immediately.

Description

Write and push data into the message queue.

Example

<?php

$workers = [];
$worker_num = 2;

for($i = 0; $i < $worker_num; $i++) {
    $process = new Swoole\Process('callback_function', FALSE);
    $process->useQueue();
    $pid = $process->start();
    $workers[$pid] = $process;
}

function callback_function(Swoole\Process $worker) {
    echo "Child process started, PID=".$worker->pid."\n";
    //recv data from the parent process
    $recv = $worker->pop();
    echo "Data received from the parent process: $recv\n";
    sleep(2);
    $worker->exit(0);
}

foreach($workers as $pid => $process) {
    $process->push("hello child process [$pid]\n");
}

for($i = 0; $i < $worker_num; $i++)
{
    $ret = Swoole\Process::wait();
    $pid = $ret['pid'];
    unset($workers[$pid]);
    echo "Child process exit, PID=".$pid.PHP_EOL;
}
Last updated on August 31, 2022