项目作者: mateusz-kolecki

项目描述 :
PHP process forking made easy
高级语言: PHP
项目地址: git://github.com/mateusz-kolecki/sub-process.git
创建时间: 2019-01-31T20:50:59Z
项目社区:https://github.com/mateusz-kolecki/sub-process

开源协议:MIT License

下载


Build Status

sub-process

PHP process forking made easy.

This library will help you to fork process and manage it state.

Install

  1. composer require mkolecki/sub-process

Usage

RealPcntl fork and communication

To fork to sub-process first create SubProcess\Process instance and then call start() method.

To controll what new Process will do, you have to pass callable.
That callable will receive Process instance which has Channel to send() and read() messages between parrent and child process.

Example examples/02-channel-communication.php:

  1. <?php
  2. include __DIR__ . '/../vendor/autoload.php';
  3. use SubProcess\Child;
  4. use SubProcess\Process;
  5. $process = new Process(function (Child $child) {
  6. $channel = $child->channel();
  7. $channel->send("Hello from child process!");
  8. $channel->send(["You", " can ", "send even arrays!"]);
  9. $object = new \stdClass();
  10. $object->inFact = "you can send any";
  11. $object->serialisable = ['value'];
  12. $channel->send($object);
  13. });
  14. $process->start();
  15. while (!$process->channel()->eof()) {
  16. var_dump($process->channel()->read());
  17. }
  18. $exitStatus = $process->wait();
  19. var_dump($exitStatus->code());
  1. $ php examples/02-channel-communication.php

Output:

  1. string(25) "Hello from child process!"
  2. array(3) {
  3. [0]=>
  4. string(3) "You"
  5. [1]=>
  6. string(5) " can "
  7. [2]=>
  8. string(17) "send even arrays!"
  9. }
  10. object(stdClass)#5 (2) {
  11. ["inFact"]=>
  12. string(16) "you can send any"
  13. ["serialisable"]=>
  14. array(1) {
  15. [0]=>
  16. string(5) "value"
  17. }
  18. }
  19. int(0)