mkolecki / sub-process

用于创建PHP新进程的库

dev-master 2019-09-20 22:30 UTC

This package is auto-updated.

Last update: 2024-09-21 21:11:49 UTC


README

Build Status

sub-process

让PHP进程创建变得简单。

这个库将帮助您创建进程并管理其状态。

安装

composer require mkolecki/sub-process

使用方法

RealPcntl进程创建和通信

首先创建SubProcess\Process实例,然后调用start()方法来创建子进程。

要控制新Process将执行的操作,您必须传递callable。这个callable将接收一个Process实例,该实例具有用于在父进程和子进程之间发送和读取消息的Channel

示例 examples/02-channel-communication.php

<?php
include __DIR__ . '/../vendor/autoload.php';

use SubProcess\Child;
use SubProcess\Process;

$process = new Process(function (Child $child) {
    $channel = $child->channel();

    $channel->send("Hello from child process!");
    $channel->send(["You", " can ", "send even arrays!"]);

    $object = new \stdClass();
    $object->inFact = "you can send any";
    $object->serialisable = ['value'];

    $channel->send($object);
});

$process->start();

while (!$process->channel()->eof()) {
    var_dump($process->channel()->read());
}

$exitStatus = $process->wait();
var_dump($exitStatus->code());
$ php examples/02-channel-communication.php

输出

string(25) "Hello from child process!"
array(3) {
  [0]=>
  string(3) "You"
  [1]=>
  string(5) " can "
  [2]=>
  string(17) "send even arrays!"
}
object(stdClass)#5 (2) {
  ["inFact"]=>
  string(16) "you can send any"
  ["serialisable"]=>
  array(1) {
    [0]=>
    string(5) "value"
  }
}
int(0)