int OpenSwoole\Process::start ( void )

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

Declaration

<?php int OpenSwoole\Process::start ( void )

Parameters

Return

Description

Create and fork the OpenSwoole Process and return PID of the process, or return FALSE if the fork is failed.

Properties of OpenSwoole Process:

<?php
$process->pid  // the pid of child process
$process->pipe // the file descriptor of pipe

Return

The pid of child process, or return FALSE if the fork is failed.

You can use swoole_errno and swoole_strerror to get the code and extra information of error.

Example

<?php
$workers = [];
$worker_num = 3;

function process(OpenSwoole\Process $process) {
    sleep(rand(5, 10));
    $process->write($process->pid);
    echo $process->pid . "\n";
}

for($i=0; $i<$worker_num; $i++) {

    $process = new OpenSwoole\Process('process');
    $pid = $process->start();
    $workers[$pid] = $process;
    echo "create a child process: $pid\n";
}

foreach($workers as $process) {
    swoole_event_add($process->pipe, function ($pipe) use($process){
        $data = $process->read();
        echo "RECV: " . $data . "\n";
    });
}

Last updated on September 1, 2022