int Swoole\Process::start ( void )

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


Latest version: pecl install openswoole-22.1.2

Declaration

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

Parameters

Return

Description

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

Properties of Swoole 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(Swoole\Process $process) {
    sleep(rand(5, 10));
    $process->write($process->pid);
    echo $process->pid . "\n";
}

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

    $process = new Swoole\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 August 31, 2022