Process

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

Introduction

OpenSwoole Process can be attached and managed by OpenSwoole Server with addProcess API.

OpenSwoole Process can be used to replace PHP pcntl extension. Compare with PHP pcntl, OpenSwoole Process provides more features:

  • IPC based on unixsock, with read/write or push/pop API.
  • Redirect standard I\O
  • The child processes are able to use OpenSwoole\Event.
  • Exec API allows the child process execute native Linux command and communicate with the parent process

You can use Process Pool or Process Manager if you like to manage a group of daemon processes and not attaching to an OpenSwoole Server.

Methods

Example

<?php
$process = new OpenSwoole\Process(function($worker){
    echo "the pid of child process is " . $worker->pid . "\n";
    echo "the file descriptor of pipe is " . $worker->pipe . "\n";

    $res = $worker->write("Hello main process\n");
    var_dump(strlen("Hello main process\n"));
    var_dump($res);

    $worker->name("php child process");
}, FALSE);

$process->start();

usleep(100);

echo $process->read();
Last updated on February 8, 2023