Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole rocess::useQueue ( int $key [, int $mode ] ) : bool
the ID of the message queue, the default value is ftok(FILE, 1)
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.
The length of data sent
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
andpop
to prepare data for the process.
<?php
$child_num = 3;
$child_processes = [];
for($i = 1; $i <= $child_num; $i++)
{
$process = new Swoole\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");
}