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

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

Declaration

<?php OpenSwoole\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 OpenSwoole\Process('callback_function', FALSE);
    $process->useQueue();
    $pid = $process->start();
    $workers[$pid] = $process;
}

function callback_function(OpenSwoole\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 = OpenSwoole\Process::wait();
    $pid = $ret['pid'];
    unset($workers[$pid]);
    echo "Child process exit, PID=".$pid.PHP_EOL;
}
Last updated on September 1, 2022