dev-master
9999999-devFork makes it simple to fork and run multiple child processes from PHP.
MIT
The Requires
- php >=5.4.1
The Development Requires
by J. Ginsberg
Fork makes it simple to fork and run multiple child processes from PHP.
Fork makes it simple to fork and run multiple child processes from PHP., (*1)
Make sure PCNTL is installed. This library only works via php-cli., (*3)
In composer.json:, (*4)
"require": { "shaggy8871/fork": "dev-master" }
Then run:, (*5)
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 may be freely distributed under the MIT license., (*6)
Fork makes it simple to fork and run multiple child processes from PHP.
MIT