Fork使从PHP中fork和运行多个子进程变得简单。

dev-master 2015-02-03 21:35 UTC

This package is auto-updated.

Last update: 2024-09-09 01:02:37 UTC


README

Fork使从PHP中fork和运行多个子进程变得简单。

Build Status

安装

确保已安装PCNTL。此库仅通过php-cli工作。

在composer.json中

"require": {
    "shaggy8871/fork": "dev-master"
}

然后运行

composer install

回调模式

<?php

include_once("vendor/autoload.php");

Fork\Fork::createChildren(['test1', 'test2'], function(Fork\ChildProcess $child) {

    // Wait 1 second to allow the broadcast to come through
    sleep(1);

    $child->sendToParent('Hello parent, I got ' . $child->getKey() . ' and "' . $child->receivedFromParent() . '" from you');

    // Wait a random amount of time
    $r = rand(1, 10);
    sleep($r);

    $child->sendToParent('Still here after ' . $r . ' seconds?');

    //... do more work

})->then(function(Fork\ParentProcess $parent) {

    $parent->broadcast('Hello children');

    // Wait for all children to finish running and handle messages
    $parent->waitForChildren(function($message, Fork\Child $child) {
        echo "Got message " . $message . " from child " . $child->getPid() . "\n";
    });

    // Display remaining output from buffer (if any)
    print_r($parent->receivedFromChildren());

    // Ask the parent to clean up after itself
    $parent->cleanup();

});

常规模式

<?php

include_once("vendor/autoload.php");

$ps = Fork::createChildren(['test1', 'test2']);

if ($ps->isParent()) {

    $ps->broadcast('Hello children');

    // Wait a second to ensure children have had a chance to fork
    sleep(1);

    // Display output from buffer
    print_r($ps->receivedFromChildren());

    // Wait for all children to finish running...
    $ps->waitForChildren();

    // Display remaining output from buffer (if any)
    print_r($ps->receivedFromChildren());

    // Ask the parent to clean up after itself
    $ps->cleanup();

    exit(0);

} else {

    $ps->sendToParent('Hello parent, I got ' . $ps->getKey() . ' and "' . $ps->receivedFromParent() . '" from you');

    sleep(5);

    $ps->sendToParent('Still here?');

    //... do work

    // Child must shut itself down properly
    $ps->shutdown();

}

联系

许可证

Fork可以在MIT许可证下自由分发。